1f10796a0Sdp-arm /* 2273b8983SGovindraj Raja * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved. 3f10796a0Sdp-arm * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5f10796a0Sdp-arm */ 6f10796a0Sdp-arm 7f10796a0Sdp-arm #include <stdint.h> 8f10796a0Sdp-arm 909d40e0eSAntonio Nino Diaz #include <common/debug.h> 1009d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h> 1176a21174SMikael Olsson #include <drivers/arm/ethosn.h> 12992f091bSAmbroise Vincent #include <lib/debugfs.h> 1309d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 14bd9344f6SAntonio Nino Diaz #include <plat/arm/common/arm_sip_svc.h> 15bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h> 1609d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 1709d40e0eSAntonio Nino Diaz 18f10796a0Sdp-arm /* ARM SiP Service UUID */ 1903364865SRoberto Vargas DEFINE_SVC_UUID2(arm_sip_svc_uid, 2003364865SRoberto Vargas 0x556d75e2, 0x6033, 0xb54b, 0xb5, 0x75, 21f10796a0Sdp-arm 0x62, 0x79, 0xfd, 0x11, 0x37, 0xff); 22f10796a0Sdp-arm 23f10796a0Sdp-arm static int arm_sip_setup(void) 24f10796a0Sdp-arm { 25*f7679d43SGovindraj Raja #if ENABLE_PMF 26992f091bSAmbroise Vincent if (pmf_setup() != 0) { 27f10796a0Sdp-arm return 1; 28992f091bSAmbroise Vincent } 29*f7679d43SGovindraj Raja #endif /* ENABLE_PMF */ 30992f091bSAmbroise Vincent 31992f091bSAmbroise Vincent #if USE_DEBUGFS 32992f091bSAmbroise Vincent 33992f091bSAmbroise Vincent if (debugfs_smc_setup() != 0) { 34992f091bSAmbroise Vincent return 1; 35992f091bSAmbroise Vincent } 36992f091bSAmbroise Vincent 37992f091bSAmbroise Vincent #endif /* USE_DEBUGFS */ 38992f091bSAmbroise Vincent 39352366edSRajasekaran Kalidoss #if ETHOSN_NPU_DRIVER 40a2cdbb1dSMikael Olsson 41a2cdbb1dSMikael Olsson if (ethosn_smc_setup() != 0) { 42a2cdbb1dSMikael Olsson return 1; 43a2cdbb1dSMikael Olsson } 44a2cdbb1dSMikael Olsson 45352366edSRajasekaran Kalidoss #endif /* ETHOSN_NPU_DRIVER */ 46a2cdbb1dSMikael Olsson 47f10796a0Sdp-arm return 0; 48f10796a0Sdp-arm } 49f10796a0Sdp-arm 50f10796a0Sdp-arm /* 51f10796a0Sdp-arm * This function handles ARM defined SiP Calls 52f10796a0Sdp-arm */ 53f10796a0Sdp-arm static uintptr_t arm_sip_handler(unsigned int smc_fid, 54f10796a0Sdp-arm u_register_t x1, 55f10796a0Sdp-arm u_register_t x2, 56f10796a0Sdp-arm u_register_t x3, 57f10796a0Sdp-arm u_register_t x4, 58f10796a0Sdp-arm void *cookie, 59f10796a0Sdp-arm void *handle, 60f10796a0Sdp-arm u_register_t flags) 61f10796a0Sdp-arm { 62b10d4499SJeenu Viswambharan int call_count = 0; 63b10d4499SJeenu Viswambharan 6476a21174SMikael Olsson #if ENABLE_PMF 65f10796a0Sdp-arm /* 66f10796a0Sdp-arm * Dispatch PMF calls to PMF SMC handler and return its return 67f10796a0Sdp-arm * value 68f10796a0Sdp-arm */ 69*f7679d43SGovindraj Raja if (is_pmf_fid_deprecated(smc_fid)) { 70*f7679d43SGovindraj Raja NOTICE("PMF Interface usage from arm-sip range is deprecated. \ 71*f7679d43SGovindraj Raja Please migrate smc call to Vendor-specific el3 range.\n"); 72f10796a0Sdp-arm return pmf_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 73f10796a0Sdp-arm handle, flags); 74f10796a0Sdp-arm } 75f10796a0Sdp-arm 7676a21174SMikael Olsson #endif /* ENABLE_PMF */ 7776a21174SMikael Olsson 78992f091bSAmbroise Vincent #if USE_DEBUGFS 79273b8983SGovindraj Raja if (is_debugfs_fid_deprecated(smc_fid)) { 80273b8983SGovindraj Raja NOTICE("Debugfs Interface usage from arm-sip range is deprecated. \ 81273b8983SGovindraj Raja Please migrate smc call to vendor-specific el3 range.\n"); 82992f091bSAmbroise Vincent return debugfs_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 83992f091bSAmbroise Vincent handle, flags); 84992f091bSAmbroise Vincent } 85992f091bSAmbroise Vincent 86992f091bSAmbroise Vincent #endif /* USE_DEBUGFS */ 87992f091bSAmbroise Vincent 88352366edSRajasekaran Kalidoss #if ETHOSN_NPU_DRIVER 8976a21174SMikael Olsson 9076a21174SMikael Olsson if (is_ethosn_fid(smc_fid)) { 9176a21174SMikael Olsson return ethosn_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 9276a21174SMikael Olsson handle, flags); 9376a21174SMikael Olsson } 9476a21174SMikael Olsson 95352366edSRajasekaran Kalidoss #endif /* ETHOSN_NPU_DRIVER */ 9676a21174SMikael Olsson 97f10796a0Sdp-arm switch (smc_fid) { 98b10d4499SJeenu Viswambharan case ARM_SIP_SVC_EXE_STATE_SWITCH: { 999d725191SBence Szépkúti /* Execution state can be switched only if EL3 is AArch64 */ 1009d725191SBence Szépkúti #ifdef __aarch64__ 101b10d4499SJeenu Viswambharan /* Allow calls from non-secure only */ 102b10d4499SJeenu Viswambharan if (!is_caller_non_secure(flags)) 103b10d4499SJeenu Viswambharan SMC_RET1(handle, STATE_SW_E_DENIED); 104b10d4499SJeenu Viswambharan 105f10796a0Sdp-arm /* 106b10d4499SJeenu Viswambharan * Pointers used in execution state switch are all 32 bits wide 107f10796a0Sdp-arm */ 108b634fa91SJeenu Viswambharan return (uintptr_t) arm_execution_state_switch(smc_fid, 109b634fa91SJeenu Viswambharan (uint32_t) x1, (uint32_t) x2, (uint32_t) x3, 110b634fa91SJeenu Viswambharan (uint32_t) x4, handle); 1119d725191SBence Szépkúti #else 1129d725191SBence Szépkúti /* State switch denied */ 1139d725191SBence Szépkúti SMC_RET1(handle, STATE_SW_E_DENIED); 1149d725191SBence Szépkúti #endif /* __aarch64__ */ 115b10d4499SJeenu Viswambharan } 116b10d4499SJeenu Viswambharan 117b10d4499SJeenu Viswambharan case ARM_SIP_SVC_CALL_COUNT: 118b10d4499SJeenu Viswambharan /* PMF calls */ 119b10d4499SJeenu Viswambharan call_count += PMF_NUM_SMC_CALLS; 120b10d4499SJeenu Viswambharan 121352366edSRajasekaran Kalidoss #if ETHOSN_NPU_DRIVER 12276a21174SMikael Olsson /* ETHOSN calls */ 12376a21174SMikael Olsson call_count += ETHOSN_NUM_SMC_CALLS; 124352366edSRajasekaran Kalidoss #endif /* ETHOSN_NPU_DRIVER */ 12576a21174SMikael Olsson 126b10d4499SJeenu Viswambharan /* State switch call */ 127b10d4499SJeenu Viswambharan call_count += 1; 128b10d4499SJeenu Viswambharan 129b10d4499SJeenu Viswambharan SMC_RET1(handle, call_count); 130f10796a0Sdp-arm 131f10796a0Sdp-arm case ARM_SIP_SVC_UID: 132f10796a0Sdp-arm /* Return UID to the caller */ 133f10796a0Sdp-arm SMC_UUID_RET(handle, arm_sip_svc_uid); 134f10796a0Sdp-arm 135f10796a0Sdp-arm case ARM_SIP_SVC_VERSION: 136f10796a0Sdp-arm /* Return the version of current implementation */ 137f10796a0Sdp-arm SMC_RET2(handle, ARM_SIP_SVC_VERSION_MAJOR, ARM_SIP_SVC_VERSION_MINOR); 138f10796a0Sdp-arm 139f10796a0Sdp-arm default: 1407a2130b4SMadhukar Pappireddy break; 141f10796a0Sdp-arm } 142f10796a0Sdp-arm 1437a2130b4SMadhukar Pappireddy /* 1447a2130b4SMadhukar Pappireddy * Fall back to allow Arm platform specific handler. 1457a2130b4SMadhukar Pappireddy * TODO: Refactor needed to move out generic handlers from this file and 1467a2130b4SMadhukar Pappireddy * only keep Arm Platform specific handlers here. 1477a2130b4SMadhukar Pappireddy */ 1487a2130b4SMadhukar Pappireddy return plat_arm_sip_handler(smc_fid, x1, x2, x3, x4, 1497a2130b4SMadhukar Pappireddy cookie, handle, flags); 150f10796a0Sdp-arm } 151f10796a0Sdp-arm 152f10796a0Sdp-arm 153f10796a0Sdp-arm /* Define a runtime service descriptor for fast SMC calls */ 154f10796a0Sdp-arm DECLARE_RT_SVC( 155f10796a0Sdp-arm arm_sip_svc, 156f10796a0Sdp-arm OEN_SIP_START, 157f10796a0Sdp-arm OEN_SIP_END, 158f10796a0Sdp-arm SMC_TYPE_FAST, 159f10796a0Sdp-arm arm_sip_setup, 160f10796a0Sdp-arm arm_sip_handler 161f10796a0Sdp-arm ); 162