Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 1 | /* |
Joshua Pimm | 6bc8067 | 2022-10-19 15:46:27 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2023, Arm Limited. All rights reserved. |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | #include <stdbool.h> |
| 9 | |
| 10 | #include <common/debug.h> |
| 11 | #include <common/runtime_svc.h> |
| 12 | #include <drivers/arm/ethosn.h> |
| 13 | #include <drivers/delay_timer.h> |
| 14 | #include <lib/mmio.h> |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 15 | #include <lib/utils_def.h> |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 16 | #include <plat/arm/common/fconf_ethosn_getter.h> |
| 17 | |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 18 | #include <platform_def.h> |
| 19 | |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 20 | #if ARM_ETHOSN_NPU_TZMP1 |
| 21 | #include "ethosn_big_fw.h" |
| 22 | #endif |
| 23 | |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 24 | /* |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 25 | * Number of Arm(R) Ethos(TM)-N NPU (NPU) devices available |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 26 | */ |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 27 | #define ETHOSN_NUM_DEVICES \ |
| 28 | FCONF_GET_PROPERTY(hw_config, ethosn_config, num_devices) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 29 | |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 30 | #define ETHOSN_GET_DEVICE(dev_idx) \ |
| 31 | FCONF_GET_PROPERTY(hw_config, ethosn_device, dev_idx) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 32 | |
| 33 | /* NPU core sec registry address */ |
| 34 | #define ETHOSN_CORE_SEC_REG(core_addr, reg_offset) \ |
| 35 | (core_addr + reg_offset) |
| 36 | |
Mikael Olsson | d6cedcb | 2023-01-27 18:53:48 +0100 | [diff] [blame] | 37 | #define ETHOSN_FW_VA_BASE 0x20000000UL |
| 38 | #define ETHOSN_WORKING_DATA_VA_BASE 0x40000000UL |
| 39 | #define ETHOSN_COMMAND_STREAM_VA_BASE 0x60000000UL |
| 40 | |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 41 | /* Reset timeout in us */ |
| 42 | #define ETHOSN_RESET_TIMEOUT_US U(10 * 1000 * 1000) |
| 43 | #define ETHOSN_RESET_WAIT_US U(1) |
| 44 | |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 45 | #define ETHOSN_AUX_FEAT_LEVEL_IRQ U(0x1) |
| 46 | #define ETHOSN_AUX_FEAT_STASHING U(0x2) |
| 47 | |
| 48 | #define SEC_AUXCTLR_REG U(0x0024) |
| 49 | #define SEC_AUXCTLR_VAL U(0x80) |
| 50 | #define SEC_AUXCTLR_LEVEL_IRQ_VAL U(0x04) |
| 51 | #define SEC_AUXCTLR_STASHING_VAL U(0xA5000000) |
| 52 | |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 53 | #define SEC_DEL_REG U(0x0004) |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 54 | #if ARM_ETHOSN_NPU_TZMP1 |
| 55 | #define SEC_DEL_VAL U(0x808) |
| 56 | #else |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 57 | #define SEC_DEL_VAL U(0x80C) |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 58 | #endif |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 59 | #define SEC_DEL_EXCC_MASK U(0x20) |
| 60 | |
| 61 | #define SEC_SECCTLR_REG U(0x0010) |
Mikael Olsson | bfd9da7 | 2023-01-11 10:36:22 +0100 | [diff] [blame] | 62 | /* Set bit[10] = 1 to workaround erratum 2838783 */ |
| 63 | #define SEC_SECCTLR_VAL U(0x403) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 64 | |
Mikael Olsson | df0bb04 | 2023-02-10 16:59:03 +0100 | [diff] [blame] | 65 | #define SEC_DEL_ADDR_EXT_REG U(0x201C) |
| 66 | #define SEC_DEL_ADDR_EXT_VAL U(0x1) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 67 | |
| 68 | #define SEC_SYSCTRL0_REG U(0x0018) |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 69 | #define SEC_SYSCTRL0_CPU_WAIT U(1) |
Mikael Olsson | 47675f2 | 2022-11-04 15:01:02 +0100 | [diff] [blame] | 70 | #define SEC_SYSCTRL0_SLEEPING U(1U << 4) |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 71 | #define SEC_SYSCTRL0_INITVTOR_MASK U(0x1FFFFF80) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 72 | #define SEC_SYSCTRL0_SOFT_RESET U(3U << 29) |
| 73 | #define SEC_SYSCTRL0_HARD_RESET U(1U << 31) |
| 74 | |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 75 | #define SEC_SYSCTRL1_REG U(0x001C) |
| 76 | #define SEC_SYSCTRL1_VAL U(0x180110) |
| 77 | |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 78 | #define SEC_NSAID_REG_BASE U(0x3004) |
| 79 | #define SEC_NSAID_OFFSET U(0x1000) |
| 80 | |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 81 | #define SEC_MMUSID_REG_BASE U(0x3008) |
| 82 | #define SEC_MMUSID_OFFSET U(0x1000) |
| 83 | |
Mikael Olsson | df0bb04 | 2023-02-10 16:59:03 +0100 | [diff] [blame] | 84 | #define SEC_ADDR_EXT_REG_BASE U(0x3018) |
| 85 | #define SEC_ADDR_EXT_OFFSET U(0x1000) |
| 86 | #define SEC_ADDR_EXT_SHIFT U(0x14) |
| 87 | #define SEC_ADDR_EXT_MASK U(0x1FFFFE00) |
| 88 | |
| 89 | #define SEC_ATTR_CTLR_REG_BASE U(0x3010) |
| 90 | #define SEC_ATTR_CTLR_OFFSET U(0x1000) |
| 91 | #define SEC_ATTR_CTLR_NUM U(9) |
| 92 | #define SEC_ATTR_CTLR_VAL U(0x1) |
| 93 | |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 94 | #define SEC_NPU_ID_REG U(0xF000) |
| 95 | #define SEC_NPU_ID_ARCH_VER_SHIFT U(0X10) |
| 96 | |
Mikael Olsson | 80b61f5 | 2023-03-14 18:29:06 +0100 | [diff] [blame] | 97 | #define FIRMWARE_STREAM_INDEX U(0x0) |
| 98 | #define WORKING_STREAM_INDEX U(0x1) |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 99 | #define PLE_STREAM_INDEX U(0x4) |
Mikael Olsson | 80b61f5 | 2023-03-14 18:29:06 +0100 | [diff] [blame] | 100 | #define INPUT_STREAM_INDEX U(0x6) |
| 101 | #define INTERMEDIATE_STREAM_INDEX U(0x7) |
| 102 | #define OUTPUT_STREAM_INDEX U(0x8) |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 103 | |
Mikael Olsson | df0bb04 | 2023-02-10 16:59:03 +0100 | [diff] [blame] | 104 | #define TO_EXTEND_ADDR(addr) \ |
| 105 | ((addr >> SEC_ADDR_EXT_SHIFT) & SEC_ADDR_EXT_MASK) |
| 106 | |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 107 | #if ARM_ETHOSN_NPU_TZMP1 |
| 108 | CASSERT(ARM_ETHOSN_NPU_FW_IMAGE_BASE > 0U, assert_ethosn_invalid_fw_image_base); |
| 109 | static const struct ethosn_big_fw *big_fw; |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 110 | |
| 111 | #define FW_INITVTOR_ADDR(big_fw) \ |
| 112 | ((ETHOSN_FW_VA_BASE + big_fw->vector_table_offset) & \ |
| 113 | SEC_SYSCTRL0_INITVTOR_MASK) |
| 114 | |
| 115 | #define SYSCTRL0_INITVTOR_ADDR(value) \ |
| 116 | (value & SEC_SYSCTRL0_INITVTOR_MASK) |
| 117 | |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 118 | #endif |
| 119 | |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 120 | static bool ethosn_get_device_and_core(uintptr_t core_addr, |
| 121 | const struct ethosn_device_t **dev_match, |
| 122 | const struct ethosn_core_t **core_match) |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 123 | { |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 124 | uint32_t dev_idx; |
| 125 | uint32_t core_idx; |
| 126 | |
| 127 | for (dev_idx = 0U; dev_idx < ETHOSN_NUM_DEVICES; ++dev_idx) { |
| 128 | const struct ethosn_device_t *dev = ETHOSN_GET_DEVICE(dev_idx); |
| 129 | |
| 130 | for (core_idx = 0U; core_idx < dev->num_cores; ++core_idx) { |
| 131 | const struct ethosn_core_t *core = &(dev->cores[core_idx]); |
| 132 | |
| 133 | if (core->addr == core_addr) { |
| 134 | *dev_match = dev; |
| 135 | *core_match = core; |
| 136 | return true; |
| 137 | } |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 141 | WARN("ETHOSN: Unknown core address given to SMC call.\n"); |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 145 | #if ARM_ETHOSN_NPU_TZMP1 |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 146 | static uint32_t ethosn_core_read_arch_version(uintptr_t core_addr) |
| 147 | { |
| 148 | uint32_t npu_id = mmio_read_32(ETHOSN_CORE_SEC_REG(core_addr, |
| 149 | SEC_NPU_ID_REG)); |
| 150 | |
| 151 | return (npu_id >> SEC_NPU_ID_ARCH_VER_SHIFT); |
| 152 | } |
| 153 | |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 154 | static void ethosn_configure_stream_nsaid(const struct ethosn_core_t *core, |
| 155 | bool is_protected) |
| 156 | { |
| 157 | size_t i; |
Mikael Olsson | 80b61f5 | 2023-03-14 18:29:06 +0100 | [diff] [blame] | 158 | uint32_t streams[9] = {[0 ... 8] = ARM_ETHOSN_NPU_NS_RO_DATA_NSAID}; |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 159 | |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 160 | streams[FIRMWARE_STREAM_INDEX] = ARM_ETHOSN_NPU_PROT_FW_NSAID; |
| 161 | streams[PLE_STREAM_INDEX] = ARM_ETHOSN_NPU_PROT_FW_NSAID; |
| 162 | |
Mikael Olsson | 80b61f5 | 2023-03-14 18:29:06 +0100 | [diff] [blame] | 163 | streams[WORKING_STREAM_INDEX] = ARM_ETHOSN_NPU_NS_RW_DATA_NSAID; |
| 164 | |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 165 | if (is_protected) { |
Mikael Olsson | 80b61f5 | 2023-03-14 18:29:06 +0100 | [diff] [blame] | 166 | streams[INPUT_STREAM_INDEX] = ARM_ETHOSN_NPU_PROT_RO_DATA_NSAID; |
| 167 | streams[INTERMEDIATE_STREAM_INDEX] = |
| 168 | ARM_ETHOSN_NPU_PROT_RW_DATA_NSAID; |
| 169 | streams[OUTPUT_STREAM_INDEX] = ARM_ETHOSN_NPU_PROT_RW_DATA_NSAID; |
| 170 | } else { |
| 171 | streams[INPUT_STREAM_INDEX] = ARM_ETHOSN_NPU_NS_RO_DATA_NSAID; |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 172 | streams[INTERMEDIATE_STREAM_INDEX] = |
Mikael Olsson | 80b61f5 | 2023-03-14 18:29:06 +0100 | [diff] [blame] | 173 | ARM_ETHOSN_NPU_NS_RW_DATA_NSAID; |
| 174 | streams[OUTPUT_STREAM_INDEX] = ARM_ETHOSN_NPU_NS_RW_DATA_NSAID; |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | for (i = 0U; i < ARRAY_SIZE(streams); ++i) { |
| 178 | const uintptr_t reg_addr = SEC_NSAID_REG_BASE + |
| 179 | (SEC_NSAID_OFFSET * i); |
| 180 | mmio_write_32(ETHOSN_CORE_SEC_REG(core->addr, reg_addr), |
| 181 | streams[i]); |
| 182 | } |
| 183 | } |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 184 | |
| 185 | static void ethosn_configure_vector_table(uintptr_t core_addr) |
| 186 | { |
| 187 | mmio_setbits_32(ETHOSN_CORE_SEC_REG(core_addr, SEC_SYSCTRL0_REG), |
| 188 | FW_INITVTOR_ADDR(big_fw)); |
| 189 | } |
| 190 | |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 191 | #endif |
| 192 | |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 193 | static void ethosn_configure_events(uintptr_t core_addr) |
| 194 | { |
| 195 | mmio_write_32(ETHOSN_CORE_SEC_REG(core_addr, SEC_SYSCTRL1_REG), SEC_SYSCTRL1_VAL); |
| 196 | } |
| 197 | |
| 198 | static bool ethosn_configure_aux_features(const struct ethosn_device_t *device, |
| 199 | uintptr_t core_addr, |
| 200 | uint32_t features) |
| 201 | { |
| 202 | uint32_t val = SEC_AUXCTLR_VAL; |
| 203 | |
| 204 | if (features & ETHOSN_AUX_FEAT_LEVEL_IRQ) { |
| 205 | val |= SEC_AUXCTLR_LEVEL_IRQ_VAL; |
| 206 | } |
| 207 | |
| 208 | if (features & ETHOSN_AUX_FEAT_STASHING) { |
| 209 | /* Stashing can't be used with reserved memory */ |
| 210 | if (device->has_reserved_memory) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | val |= SEC_AUXCTLR_STASHING_VAL; |
| 215 | } |
| 216 | |
| 217 | mmio_setbits_32(ETHOSN_CORE_SEC_REG(core_addr, SEC_AUXCTLR_REG), val); |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 222 | static void ethosn_configure_smmu_streams(const struct ethosn_device_t *device, |
| 223 | const struct ethosn_core_t *core, |
| 224 | uint32_t asset_alloc_idx) |
| 225 | { |
| 226 | const struct ethosn_main_allocator_t *main_alloc = |
| 227 | &(core->main_allocator); |
| 228 | const struct ethosn_asset_allocator_t *asset_alloc = |
| 229 | &(device->asset_allocators[asset_alloc_idx]); |
| 230 | const uint32_t streams[9] = { |
| 231 | main_alloc->firmware.stream_id, |
| 232 | main_alloc->working_data.stream_id, |
| 233 | asset_alloc->command_stream.stream_id, |
| 234 | 0U, /* Not used*/ |
| 235 | main_alloc->firmware.stream_id, |
| 236 | asset_alloc->weight_data.stream_id, |
| 237 | asset_alloc->buffer_data.stream_id, |
| 238 | asset_alloc->intermediate_data.stream_id, |
| 239 | asset_alloc->buffer_data.stream_id |
| 240 | }; |
| 241 | size_t i; |
| 242 | |
| 243 | for (i = 0U; i < ARRAY_SIZE(streams); ++i) { |
| 244 | const uintptr_t reg_addr = SEC_MMUSID_REG_BASE + |
| 245 | (SEC_MMUSID_OFFSET * i); |
| 246 | mmio_write_32(ETHOSN_CORE_SEC_REG(core->addr, reg_addr), |
| 247 | streams[i]); |
| 248 | } |
| 249 | } |
| 250 | |
Mikael Olsson | df0bb04 | 2023-02-10 16:59:03 +0100 | [diff] [blame] | 251 | static void ethosn_configure_stream_addr_extends(const struct ethosn_device_t *device, |
| 252 | uintptr_t core_addr) |
| 253 | { |
| 254 | uint32_t addr_extends[3] = { 0 }; |
| 255 | size_t i; |
| 256 | |
| 257 | if (device->has_reserved_memory) { |
| 258 | const uint32_t addr = TO_EXTEND_ADDR(device->reserved_memory_addr); |
| 259 | |
| 260 | addr_extends[0] = addr; |
| 261 | addr_extends[1] = addr; |
| 262 | addr_extends[2] = addr; |
| 263 | } else { |
| 264 | addr_extends[0] = TO_EXTEND_ADDR(ETHOSN_FW_VA_BASE); |
| 265 | addr_extends[1] = TO_EXTEND_ADDR(ETHOSN_WORKING_DATA_VA_BASE); |
| 266 | addr_extends[2] = TO_EXTEND_ADDR(ETHOSN_COMMAND_STREAM_VA_BASE); |
| 267 | } |
| 268 | |
| 269 | for (i = 0U; i < ARRAY_SIZE(addr_extends); ++i) { |
| 270 | const uintptr_t reg_addr = SEC_ADDR_EXT_REG_BASE + |
| 271 | (SEC_ADDR_EXT_OFFSET * i); |
| 272 | mmio_write_32(ETHOSN_CORE_SEC_REG(core_addr, reg_addr), |
| 273 | addr_extends[i]); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static void ethosn_configure_stream_attr_ctlr(uintptr_t core_addr) |
| 278 | { |
| 279 | size_t i; |
| 280 | |
| 281 | for (i = 0U; i < SEC_ATTR_CTLR_NUM; ++i) { |
| 282 | const uintptr_t reg_addr = SEC_ATTR_CTLR_REG_BASE + |
| 283 | (SEC_ATTR_CTLR_OFFSET * i); |
| 284 | mmio_write_32(ETHOSN_CORE_SEC_REG(core_addr, reg_addr), |
| 285 | SEC_ATTR_CTLR_VAL); |
| 286 | } |
| 287 | } |
| 288 | |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 289 | static void ethosn_delegate_to_ns(uintptr_t core_addr) |
| 290 | { |
| 291 | mmio_setbits_32(ETHOSN_CORE_SEC_REG(core_addr, SEC_SECCTLR_REG), |
| 292 | SEC_SECCTLR_VAL); |
| 293 | |
| 294 | mmio_setbits_32(ETHOSN_CORE_SEC_REG(core_addr, SEC_DEL_REG), |
| 295 | SEC_DEL_VAL); |
| 296 | |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 297 | mmio_setbits_32(ETHOSN_CORE_SEC_REG(core_addr, SEC_DEL_ADDR_EXT_REG), |
| 298 | SEC_DEL_ADDR_EXT_VAL); |
| 299 | } |
| 300 | |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 301 | static int ethosn_is_sec(uintptr_t core_addr) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 302 | { |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 303 | if ((mmio_read_32(ETHOSN_CORE_SEC_REG(core_addr, SEC_DEL_REG)) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 304 | & SEC_DEL_EXCC_MASK) != 0U) { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | return 1; |
| 309 | } |
| 310 | |
Mikael Olsson | 47675f2 | 2022-11-04 15:01:02 +0100 | [diff] [blame] | 311 | static int ethosn_core_is_sleeping(uintptr_t core_addr) |
| 312 | { |
| 313 | const uintptr_t sysctrl0_reg = |
| 314 | ETHOSN_CORE_SEC_REG(core_addr, SEC_SYSCTRL0_REG); |
| 315 | const uint32_t sleeping_mask = SEC_SYSCTRL0_SLEEPING; |
| 316 | |
| 317 | return ((mmio_read_32(sysctrl0_reg) & sleeping_mask) == sleeping_mask); |
| 318 | } |
| 319 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 320 | static bool ethosn_core_reset(uintptr_t core_addr, bool hard_reset) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 321 | { |
| 322 | unsigned int timeout; |
| 323 | const uintptr_t sysctrl0_reg = |
| 324 | ETHOSN_CORE_SEC_REG(core_addr, SEC_SYSCTRL0_REG); |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 325 | const uint32_t reset_val = hard_reset ? SEC_SYSCTRL0_HARD_RESET : |
| 326 | SEC_SYSCTRL0_SOFT_RESET; |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 327 | |
| 328 | mmio_write_32(sysctrl0_reg, reset_val); |
| 329 | |
| 330 | /* Wait for reset to complete */ |
| 331 | for (timeout = 0U; timeout < ETHOSN_RESET_TIMEOUT_US; |
| 332 | timeout += ETHOSN_RESET_WAIT_US) { |
| 333 | |
| 334 | if ((mmio_read_32(sysctrl0_reg) & reset_val) == 0U) { |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | udelay(ETHOSN_RESET_WAIT_US); |
| 339 | } |
| 340 | |
| 341 | return timeout < ETHOSN_RESET_TIMEOUT_US; |
| 342 | } |
| 343 | |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 344 | static int ethosn_core_boot_fw(uintptr_t core_addr) |
| 345 | { |
| 346 | #if ARM_ETHOSN_NPU_TZMP1 |
| 347 | const uintptr_t sysctrl0_reg = ETHOSN_CORE_SEC_REG(core_addr, SEC_SYSCTRL0_REG); |
| 348 | const uint32_t sysctrl0_val = mmio_read_32(sysctrl0_reg); |
| 349 | const bool waiting = (sysctrl0_val & SEC_SYSCTRL0_CPU_WAIT); |
| 350 | |
| 351 | if (!waiting) { |
| 352 | WARN("ETHOSN: Firmware is already running.\n"); |
| 353 | return ETHOSN_INVALID_STATE; |
| 354 | } |
| 355 | |
| 356 | if (SYSCTRL0_INITVTOR_ADDR(sysctrl0_val) != FW_INITVTOR_ADDR(big_fw)) { |
| 357 | WARN("ETHOSN: Unknown vector table won't boot firmware.\n"); |
| 358 | return ETHOSN_INVALID_CONFIGURATION; |
| 359 | } |
| 360 | |
| 361 | mmio_clrbits_32(sysctrl0_reg, SEC_SYSCTRL0_CPU_WAIT); |
| 362 | |
| 363 | return ETHOSN_SUCCESS; |
| 364 | #else |
| 365 | return ETHOSN_NOT_SUPPORTED; |
| 366 | #endif |
| 367 | } |
| 368 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 369 | static int ethosn_core_full_reset(const struct ethosn_device_t *device, |
| 370 | const struct ethosn_core_t *core, |
| 371 | bool hard_reset, |
| 372 | u_register_t asset_alloc_idx, |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 373 | u_register_t is_protected, |
| 374 | u_register_t aux_features) |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 375 | { |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 376 | if (!device->has_reserved_memory && |
| 377 | asset_alloc_idx >= device->num_allocators) { |
| 378 | WARN("ETHOSN: Unknown asset allocator index given to SMC call.\n"); |
| 379 | return ETHOSN_UNKNOWN_ALLOCATOR_IDX; |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 382 | if (!ethosn_core_reset(core->addr, hard_reset)) { |
| 383 | return ETHOSN_FAILURE; |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 386 | if (!ethosn_configure_aux_features(device, core->addr, aux_features)) { |
| 387 | return ETHOSN_INVALID_CONFIGURATION; |
| 388 | } |
| 389 | |
| 390 | ethosn_configure_events(core->addr); |
| 391 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 392 | if (!device->has_reserved_memory) { |
| 393 | ethosn_configure_smmu_streams(device, core, asset_alloc_idx); |
| 394 | |
| 395 | #if ARM_ETHOSN_NPU_TZMP1 |
| 396 | ethosn_configure_stream_nsaid(core, is_protected); |
| 397 | #endif |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Mikael Olsson | df0bb04 | 2023-02-10 16:59:03 +0100 | [diff] [blame] | 400 | ethosn_configure_stream_addr_extends(device, core->addr); |
| 401 | ethosn_configure_stream_attr_ctlr(core->addr); |
| 402 | |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 403 | #if ARM_ETHOSN_NPU_TZMP1 |
| 404 | ethosn_configure_vector_table(core->addr); |
| 405 | #endif |
| 406 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 407 | ethosn_delegate_to_ns(core->addr); |
| 408 | |
| 409 | return ETHOSN_SUCCESS; |
| 410 | } |
| 411 | |
| 412 | static uintptr_t ethosn_smc_core_reset_handler(const struct ethosn_device_t *device, |
| 413 | const struct ethosn_core_t *core, |
| 414 | bool hard_reset, |
| 415 | u_register_t asset_alloc_idx, |
| 416 | u_register_t reset_type, |
| 417 | u_register_t is_protected, |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 418 | u_register_t aux_features, |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 419 | void *handle) |
| 420 | { |
| 421 | int ret; |
| 422 | |
| 423 | switch (reset_type) { |
| 424 | case ETHOSN_RESET_TYPE_FULL: |
| 425 | ret = ethosn_core_full_reset(device, core, hard_reset, |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 426 | asset_alloc_idx, is_protected, |
| 427 | aux_features); |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 428 | break; |
| 429 | case ETHOSN_RESET_TYPE_HALT: |
| 430 | ret = ethosn_core_reset(core->addr, hard_reset) ? ETHOSN_SUCCESS : ETHOSN_FAILURE; |
| 431 | break; |
| 432 | default: |
| 433 | WARN("ETHOSN: Invalid reset type given to SMC call.\n"); |
| 434 | ret = ETHOSN_INVALID_PARAMETER; |
| 435 | break; |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 438 | SMC_RET1(handle, ret); |
| 439 | } |
| 440 | |
| 441 | static uintptr_t ethosn_smc_core_handler(uint32_t fid, |
| 442 | u_register_t core_addr, |
| 443 | u_register_t asset_alloc_idx, |
| 444 | u_register_t reset_type, |
| 445 | u_register_t is_protected, |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 446 | u_register_t aux_features, |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 447 | void *handle) |
| 448 | { |
| 449 | bool hard_reset = false; |
| 450 | const struct ethosn_device_t *device = NULL; |
| 451 | const struct ethosn_core_t *core = NULL; |
| 452 | |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 453 | if (!ethosn_get_device_and_core(core_addr, &device, &core)) { |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 454 | SMC_RET1(handle, ETHOSN_UNKNOWN_CORE_ADDRESS); |
| 455 | } |
| 456 | |
Laurent Carlier | 5205df2 | 2021-09-16 15:10:35 +0100 | [diff] [blame] | 457 | switch (fid) { |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 458 | case ETHOSN_FNUM_IS_SEC: |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 459 | SMC_RET1(handle, ethosn_is_sec(core->addr)); |
Mikael Olsson | 47675f2 | 2022-11-04 15:01:02 +0100 | [diff] [blame] | 460 | case ETHOSN_FNUM_IS_SLEEPING: |
| 461 | SMC_RET1(handle, ethosn_core_is_sleeping(core->addr)); |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 462 | case ETHOSN_FNUM_HARD_RESET: |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 463 | hard_reset = true; |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 464 | /* Fallthrough */ |
| 465 | case ETHOSN_FNUM_SOFT_RESET: |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 466 | return ethosn_smc_core_reset_handler(device, core, |
| 467 | hard_reset, |
| 468 | asset_alloc_idx, |
| 469 | reset_type, |
| 470 | is_protected, |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 471 | aux_features, |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 472 | handle); |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 473 | case ETHOSN_FNUM_BOOT_FW: |
| 474 | SMC_RET1(handle, ethosn_core_boot_fw(core->addr)); |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 475 | default: |
| 476 | WARN("ETHOSN: Unimplemented SMC call: 0x%x\n", fid); |
| 477 | SMC_RET1(handle, SMC_UNK); |
| 478 | } |
| 479 | } |
| 480 | |
Mikael Olsson | d6cedcb | 2023-01-27 18:53:48 +0100 | [diff] [blame] | 481 | static uintptr_t ethosn_smc_fw_prop_handler(u_register_t fw_property, |
| 482 | void *handle) |
| 483 | { |
| 484 | #if ARM_ETHOSN_NPU_TZMP1 |
| 485 | switch (fw_property) { |
| 486 | case ETHOSN_FW_PROP_VERSION: |
| 487 | SMC_RET4(handle, ETHOSN_SUCCESS, |
| 488 | big_fw->fw_ver_major, |
| 489 | big_fw->fw_ver_minor, |
| 490 | big_fw->fw_ver_patch); |
| 491 | case ETHOSN_FW_PROP_MEM_INFO: |
| 492 | SMC_RET3(handle, ETHOSN_SUCCESS, |
| 493 | ((void *)big_fw) + big_fw->offset, |
| 494 | big_fw->size); |
| 495 | case ETHOSN_FW_PROP_OFFSETS: |
| 496 | SMC_RET3(handle, ETHOSN_SUCCESS, |
| 497 | big_fw->ple_offset, |
| 498 | big_fw->unpriv_stack_offset); |
| 499 | case ETHOSN_FW_PROP_VA_MAP: |
| 500 | SMC_RET4(handle, ETHOSN_SUCCESS, |
| 501 | ETHOSN_FW_VA_BASE, |
| 502 | ETHOSN_WORKING_DATA_VA_BASE, |
| 503 | ETHOSN_COMMAND_STREAM_VA_BASE); |
| 504 | default: |
| 505 | WARN("ETHOSN: Unknown firmware property\n"); |
| 506 | SMC_RET1(handle, ETHOSN_INVALID_PARAMETER); |
| 507 | } |
| 508 | #else |
| 509 | SMC_RET1(handle, ETHOSN_NOT_SUPPORTED); |
| 510 | #endif |
| 511 | } |
| 512 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 513 | uintptr_t ethosn_smc_handler(uint32_t smc_fid, |
| 514 | u_register_t x1, |
| 515 | u_register_t x2, |
| 516 | u_register_t x3, |
| 517 | u_register_t x4, |
| 518 | void *cookie, |
| 519 | void *handle, |
| 520 | u_register_t flags) |
| 521 | { |
| 522 | const uint32_t fid = smc_fid & FUNCID_NUM_MASK; |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 523 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 524 | /* Only SiP fast calls are expected */ |
| 525 | if ((GET_SMC_TYPE(smc_fid) != SMC_TYPE_FAST) || |
| 526 | (GET_SMC_OEN(smc_fid) != OEN_SIP_START)) { |
| 527 | SMC_RET1(handle, SMC_UNK); |
| 528 | } |
Rajasekaran Kalidoss | f8a18b8 | 2022-11-16 17:16:44 +0100 | [diff] [blame] | 529 | |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 530 | /* Truncate parameters to 32-bits for SMC32 */ |
| 531 | if (GET_SMC_CC(smc_fid) == SMC_32) { |
| 532 | x1 &= 0xFFFFFFFF; |
| 533 | x2 &= 0xFFFFFFFF; |
| 534 | x3 &= 0xFFFFFFFF; |
| 535 | x4 &= 0xFFFFFFFF; |
| 536 | } |
Mikael Olsson | 3288b46 | 2022-08-15 17:12:58 +0200 | [diff] [blame] | 537 | |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 538 | if (!is_ethosn_fid(smc_fid) || (fid > ETHOSN_FNUM_BOOT_FW)) { |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 539 | WARN("ETHOSN: Unknown SMC call: 0x%x\n", smc_fid); |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 540 | SMC_RET1(handle, SMC_UNK); |
| 541 | } |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 542 | |
Mikael Olsson | d6cedcb | 2023-01-27 18:53:48 +0100 | [diff] [blame] | 543 | switch (fid) { |
| 544 | case ETHOSN_FNUM_VERSION: |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 545 | SMC_RET2(handle, ETHOSN_VERSION_MAJOR, ETHOSN_VERSION_MINOR); |
Mikael Olsson | d6cedcb | 2023-01-27 18:53:48 +0100 | [diff] [blame] | 546 | case ETHOSN_FNUM_GET_FW_PROP: |
| 547 | return ethosn_smc_fw_prop_handler(x1, handle); |
Mikael Olsson | f663803 | 2023-01-27 18:26:36 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Mikael Olsson | fe9677f | 2023-02-10 11:36:19 +0100 | [diff] [blame] | 550 | return ethosn_smc_core_handler(fid, x1, x2, x3, x4, |
| 551 | SMC_GET_GP(handle, CTX_GPREG_X5), |
| 552 | handle); |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 553 | } |
Mikael Olsson | 461bf7d | 2023-01-18 18:05:15 +0100 | [diff] [blame] | 554 | |
| 555 | int ethosn_smc_setup(void) |
| 556 | { |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 557 | #if ARM_ETHOSN_NPU_TZMP1 |
| 558 | struct ethosn_device_t *dev; |
| 559 | uint32_t arch_ver; |
| 560 | #endif |
| 561 | |
Mikael Olsson | 461bf7d | 2023-01-18 18:05:15 +0100 | [diff] [blame] | 562 | if (ETHOSN_NUM_DEVICES == 0U) { |
| 563 | ERROR("ETHOSN: No NPU found\n"); |
| 564 | return ETHOSN_FAILURE; |
| 565 | } |
| 566 | |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 567 | #if ARM_ETHOSN_NPU_TZMP1 |
| 568 | |
| 569 | /* Only one NPU core is supported in the TZMP1 setup */ |
| 570 | if ((ETHOSN_NUM_DEVICES != 1U) || |
| 571 | (ETHOSN_GET_DEVICE(0U)->num_cores != 1U)) { |
| 572 | ERROR("ETHOSN: TZMP1 doesn't support multiple NPU cores\n"); |
| 573 | return ETHOSN_FAILURE; |
| 574 | } |
| 575 | |
| 576 | dev = ETHOSN_GET_DEVICE(0U); |
Mikael Olsson | c7d3a27 | 2023-02-10 16:59:23 +0100 | [diff] [blame] | 577 | if (dev->has_reserved_memory) { |
| 578 | ERROR("ETHOSN: TZMP1 doesn't support using reserved memory\n"); |
| 579 | return ETHOSN_FAILURE; |
| 580 | } |
| 581 | |
Mikael Olsson | a7df0d6 | 2023-01-13 09:56:41 +0100 | [diff] [blame] | 582 | arch_ver = ethosn_core_read_arch_version(dev->cores[0U].addr); |
| 583 | big_fw = (struct ethosn_big_fw *)ARM_ETHOSN_NPU_FW_IMAGE_BASE; |
| 584 | |
| 585 | if (!ethosn_big_fw_verify_header(big_fw, arch_ver)) { |
| 586 | return ETHOSN_FAILURE; |
| 587 | } |
| 588 | |
| 589 | NOTICE("ETHOSN: TZMP1 setup succeeded with firmware version %u.%u.%u\n", |
| 590 | big_fw->fw_ver_major, big_fw->fw_ver_minor, |
| 591 | big_fw->fw_ver_patch); |
| 592 | #else |
| 593 | NOTICE("ETHOSN: Setup succeeded\n"); |
| 594 | #endif |
| 595 | |
Mikael Olsson | 461bf7d | 2023-01-18 18:05:15 +0100 | [diff] [blame] | 596 | return 0; |
| 597 | } |