xref: /rk3399_ARM-atf/plat/arm/common/arm_sip_svc.c (revision 273b898388adde0d7cc858fab6774b1da61caa0f)
1f10796a0Sdp-arm /*
2*273b8983SGovindraj 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 {
25992f091bSAmbroise Vincent 	if (pmf_setup() != 0) {
26f10796a0Sdp-arm 		return 1;
27992f091bSAmbroise Vincent 	}
28992f091bSAmbroise Vincent 
29992f091bSAmbroise Vincent #if USE_DEBUGFS
30992f091bSAmbroise Vincent 
31992f091bSAmbroise Vincent 	if (debugfs_smc_setup() != 0) {
32992f091bSAmbroise Vincent 		return 1;
33992f091bSAmbroise Vincent 	}
34992f091bSAmbroise Vincent 
35992f091bSAmbroise Vincent #endif /* USE_DEBUGFS */
36992f091bSAmbroise Vincent 
37352366edSRajasekaran Kalidoss #if ETHOSN_NPU_DRIVER
38a2cdbb1dSMikael Olsson 
39a2cdbb1dSMikael Olsson 	if (ethosn_smc_setup() != 0) {
40a2cdbb1dSMikael Olsson 		return 1;
41a2cdbb1dSMikael Olsson 	}
42a2cdbb1dSMikael Olsson 
43352366edSRajasekaran Kalidoss #endif /* ETHOSN_NPU_DRIVER */
44a2cdbb1dSMikael Olsson 
45f10796a0Sdp-arm 	return 0;
46f10796a0Sdp-arm }
47f10796a0Sdp-arm 
48f10796a0Sdp-arm /*
49f10796a0Sdp-arm  * This function handles ARM defined SiP Calls
50f10796a0Sdp-arm  */
51f10796a0Sdp-arm static uintptr_t arm_sip_handler(unsigned int smc_fid,
52f10796a0Sdp-arm 			u_register_t x1,
53f10796a0Sdp-arm 			u_register_t x2,
54f10796a0Sdp-arm 			u_register_t x3,
55f10796a0Sdp-arm 			u_register_t x4,
56f10796a0Sdp-arm 			void *cookie,
57f10796a0Sdp-arm 			void *handle,
58f10796a0Sdp-arm 			u_register_t flags)
59f10796a0Sdp-arm {
60b10d4499SJeenu Viswambharan 	int call_count = 0;
61b10d4499SJeenu Viswambharan 
6276a21174SMikael Olsson #if ENABLE_PMF
6376a21174SMikael Olsson 
64f10796a0Sdp-arm 	/*
65f10796a0Sdp-arm 	 * Dispatch PMF calls to PMF SMC handler and return its return
66f10796a0Sdp-arm 	 * value
67f10796a0Sdp-arm 	 */
68f10796a0Sdp-arm 	if (is_pmf_fid(smc_fid)) {
69f10796a0Sdp-arm 		return pmf_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
70f10796a0Sdp-arm 				handle, flags);
71f10796a0Sdp-arm 	}
72f10796a0Sdp-arm 
7376a21174SMikael Olsson #endif /* ENABLE_PMF */
7476a21174SMikael Olsson 
75992f091bSAmbroise Vincent #if USE_DEBUGFS
76*273b8983SGovindraj Raja 	if (is_debugfs_fid_deprecated(smc_fid)) {
77*273b8983SGovindraj Raja 		NOTICE("Debugfs Interface usage from arm-sip range is deprecated. \
78*273b8983SGovindraj Raja 			Please migrate smc call to vendor-specific el3 range.\n");
79992f091bSAmbroise Vincent 		return debugfs_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
80992f091bSAmbroise Vincent 					   handle, flags);
81992f091bSAmbroise Vincent 	}
82992f091bSAmbroise Vincent 
83992f091bSAmbroise Vincent #endif /* USE_DEBUGFS */
84992f091bSAmbroise Vincent 
85352366edSRajasekaran Kalidoss #if ETHOSN_NPU_DRIVER
8676a21174SMikael Olsson 
8776a21174SMikael Olsson 	if (is_ethosn_fid(smc_fid)) {
8876a21174SMikael Olsson 		return ethosn_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
8976a21174SMikael Olsson 					  handle, flags);
9076a21174SMikael Olsson 	}
9176a21174SMikael Olsson 
92352366edSRajasekaran Kalidoss #endif /* ETHOSN_NPU_DRIVER */
9376a21174SMikael Olsson 
94f10796a0Sdp-arm 	switch (smc_fid) {
95b10d4499SJeenu Viswambharan 	case ARM_SIP_SVC_EXE_STATE_SWITCH: {
969d725191SBence Szépkúti 		/* Execution state can be switched only if EL3 is AArch64 */
979d725191SBence Szépkúti #ifdef __aarch64__
98b10d4499SJeenu Viswambharan 		/* Allow calls from non-secure only */
99b10d4499SJeenu Viswambharan 		if (!is_caller_non_secure(flags))
100b10d4499SJeenu Viswambharan 			SMC_RET1(handle, STATE_SW_E_DENIED);
101b10d4499SJeenu Viswambharan 
102f10796a0Sdp-arm 		/*
103b10d4499SJeenu Viswambharan 		 * Pointers used in execution state switch are all 32 bits wide
104f10796a0Sdp-arm 		 */
105b634fa91SJeenu Viswambharan 		return (uintptr_t) arm_execution_state_switch(smc_fid,
106b634fa91SJeenu Viswambharan 				(uint32_t) x1, (uint32_t) x2, (uint32_t) x3,
107b634fa91SJeenu Viswambharan 				(uint32_t) x4, handle);
1089d725191SBence Szépkúti #else
1099d725191SBence Szépkúti 		/* State switch denied */
1109d725191SBence Szépkúti 		SMC_RET1(handle, STATE_SW_E_DENIED);
1119d725191SBence Szépkúti #endif /* __aarch64__ */
112b10d4499SJeenu Viswambharan 		}
113b10d4499SJeenu Viswambharan 
114b10d4499SJeenu Viswambharan 	case ARM_SIP_SVC_CALL_COUNT:
115b10d4499SJeenu Viswambharan 		/* PMF calls */
116b10d4499SJeenu Viswambharan 		call_count += PMF_NUM_SMC_CALLS;
117b10d4499SJeenu Viswambharan 
118352366edSRajasekaran Kalidoss #if ETHOSN_NPU_DRIVER
11976a21174SMikael Olsson 		/* ETHOSN calls */
12076a21174SMikael Olsson 		call_count += ETHOSN_NUM_SMC_CALLS;
121352366edSRajasekaran Kalidoss #endif          /* ETHOSN_NPU_DRIVER */
12276a21174SMikael Olsson 
123b10d4499SJeenu Viswambharan 		/* State switch call */
124b10d4499SJeenu Viswambharan 		call_count += 1;
125b10d4499SJeenu Viswambharan 
126b10d4499SJeenu Viswambharan 		SMC_RET1(handle, call_count);
127f10796a0Sdp-arm 
128f10796a0Sdp-arm 	case ARM_SIP_SVC_UID:
129f10796a0Sdp-arm 		/* Return UID to the caller */
130f10796a0Sdp-arm 		SMC_UUID_RET(handle, arm_sip_svc_uid);
131f10796a0Sdp-arm 
132f10796a0Sdp-arm 	case ARM_SIP_SVC_VERSION:
133f10796a0Sdp-arm 		/* Return the version of current implementation */
134f10796a0Sdp-arm 		SMC_RET2(handle, ARM_SIP_SVC_VERSION_MAJOR, ARM_SIP_SVC_VERSION_MINOR);
135f10796a0Sdp-arm 
136f10796a0Sdp-arm 	default:
1377a2130b4SMadhukar Pappireddy 		break;
138f10796a0Sdp-arm 	}
139f10796a0Sdp-arm 
1407a2130b4SMadhukar Pappireddy 	/*
1417a2130b4SMadhukar Pappireddy 	 * Fall back to allow Arm platform specific handler.
1427a2130b4SMadhukar Pappireddy 	 * TODO: Refactor needed to move out generic handlers from this file and
1437a2130b4SMadhukar Pappireddy 	 * only keep Arm Platform specific handlers here.
1447a2130b4SMadhukar Pappireddy 	 */
1457a2130b4SMadhukar Pappireddy 	return plat_arm_sip_handler(smc_fid, x1, x2, x3, x4,
1467a2130b4SMadhukar Pappireddy 					cookie, handle, flags);
147f10796a0Sdp-arm }
148f10796a0Sdp-arm 
149f10796a0Sdp-arm 
150f10796a0Sdp-arm /* Define a runtime service descriptor for fast SMC calls */
151f10796a0Sdp-arm DECLARE_RT_SVC(
152f10796a0Sdp-arm 	arm_sip_svc,
153f10796a0Sdp-arm 	OEN_SIP_START,
154f10796a0Sdp-arm 	OEN_SIP_END,
155f10796a0Sdp-arm 	SMC_TYPE_FAST,
156f10796a0Sdp-arm 	arm_sip_setup,
157f10796a0Sdp-arm 	arm_sip_handler
158f10796a0Sdp-arm );
159