ethosn_smc.c 16 KB

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