xref: /rk3399_ARM-atf/plat/arm/common/arm_sip_svc.c (revision a2cdbb1df088cde410aea1d5989dfc500aaf7939)
1f10796a0Sdp-arm /*
2*a2cdbb1dSMikael Olsson  * Copyright (c) 2016-2023, 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 
37*a2cdbb1dSMikael Olsson #if ARM_ETHOSN_NPU_DRIVER
38*a2cdbb1dSMikael Olsson 
39*a2cdbb1dSMikael Olsson 	if (ethosn_smc_setup() != 0) {
40*a2cdbb1dSMikael Olsson 		return 1;
41*a2cdbb1dSMikael Olsson 	}
42*a2cdbb1dSMikael Olsson 
43*a2cdbb1dSMikael Olsson #endif /* ARM_ETHOSN_NPU_DRIVER */
44*a2cdbb1dSMikael 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
76992f091bSAmbroise Vincent 
77992f091bSAmbroise Vincent 	if (is_debugfs_fid(smc_fid)) {
78992f091bSAmbroise Vincent 		return debugfs_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
79992f091bSAmbroise Vincent 					   handle, flags);
80992f091bSAmbroise Vincent 	}
81992f091bSAmbroise Vincent 
82992f091bSAmbroise Vincent #endif /* USE_DEBUGFS */
83992f091bSAmbroise Vincent 
8476a21174SMikael Olsson #if ARM_ETHOSN_NPU_DRIVER
8576a21174SMikael Olsson 
8676a21174SMikael Olsson 	if (is_ethosn_fid(smc_fid)) {
8776a21174SMikael Olsson 		return ethosn_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
8876a21174SMikael Olsson 					  handle, flags);
8976a21174SMikael Olsson 	}
9076a21174SMikael Olsson 
9176a21174SMikael Olsson #endif /* ARM_ETHOSN_NPU_DRIVER */
9276a21174SMikael Olsson 
93f10796a0Sdp-arm 	switch (smc_fid) {
94b10d4499SJeenu Viswambharan 	case ARM_SIP_SVC_EXE_STATE_SWITCH: {
959d725191SBence Szépkúti 		/* Execution state can be switched only if EL3 is AArch64 */
969d725191SBence Szépkúti #ifdef __aarch64__
97b10d4499SJeenu Viswambharan 		/* Allow calls from non-secure only */
98b10d4499SJeenu Viswambharan 		if (!is_caller_non_secure(flags))
99b10d4499SJeenu Viswambharan 			SMC_RET1(handle, STATE_SW_E_DENIED);
100b10d4499SJeenu Viswambharan 
101f10796a0Sdp-arm 		/*
102b10d4499SJeenu Viswambharan 		 * Pointers used in execution state switch are all 32 bits wide
103f10796a0Sdp-arm 		 */
104b634fa91SJeenu Viswambharan 		return (uintptr_t) arm_execution_state_switch(smc_fid,
105b634fa91SJeenu Viswambharan 				(uint32_t) x1, (uint32_t) x2, (uint32_t) x3,
106b634fa91SJeenu Viswambharan 				(uint32_t) x4, handle);
1079d725191SBence Szépkúti #else
1089d725191SBence Szépkúti 		/* State switch denied */
1099d725191SBence Szépkúti 		SMC_RET1(handle, STATE_SW_E_DENIED);
1109d725191SBence Szépkúti #endif /* __aarch64__ */
111b10d4499SJeenu Viswambharan 		}
112b10d4499SJeenu Viswambharan 
113b10d4499SJeenu Viswambharan 	case ARM_SIP_SVC_CALL_COUNT:
114b10d4499SJeenu Viswambharan 		/* PMF calls */
115b10d4499SJeenu Viswambharan 		call_count += PMF_NUM_SMC_CALLS;
116b10d4499SJeenu Viswambharan 
11776a21174SMikael Olsson #if ARM_ETHOSN_NPU_DRIVER
11876a21174SMikael Olsson 		/* ETHOSN calls */
11976a21174SMikael Olsson 		call_count += ETHOSN_NUM_SMC_CALLS;
12076a21174SMikael Olsson #endif /* ARM_ETHOSN_NPU_DRIVER */
12176a21174SMikael Olsson 
122b10d4499SJeenu Viswambharan 		/* State switch call */
123b10d4499SJeenu Viswambharan 		call_count += 1;
124b10d4499SJeenu Viswambharan 
125b10d4499SJeenu Viswambharan 		SMC_RET1(handle, call_count);
126f10796a0Sdp-arm 
127f10796a0Sdp-arm 	case ARM_SIP_SVC_UID:
128f10796a0Sdp-arm 		/* Return UID to the caller */
129f10796a0Sdp-arm 		SMC_UUID_RET(handle, arm_sip_svc_uid);
130f10796a0Sdp-arm 
131f10796a0Sdp-arm 	case ARM_SIP_SVC_VERSION:
132f10796a0Sdp-arm 		/* Return the version of current implementation */
133f10796a0Sdp-arm 		SMC_RET2(handle, ARM_SIP_SVC_VERSION_MAJOR, ARM_SIP_SVC_VERSION_MINOR);
134f10796a0Sdp-arm 
135f10796a0Sdp-arm 	default:
136f10796a0Sdp-arm 		WARN("Unimplemented ARM SiP Service Call: 0x%x \n", smc_fid);
137f10796a0Sdp-arm 		SMC_RET1(handle, SMC_UNK);
138f10796a0Sdp-arm 	}
139f10796a0Sdp-arm 
140f10796a0Sdp-arm }
141f10796a0Sdp-arm 
142f10796a0Sdp-arm 
143f10796a0Sdp-arm /* Define a runtime service descriptor for fast SMC calls */
144f10796a0Sdp-arm DECLARE_RT_SVC(
145f10796a0Sdp-arm 	arm_sip_svc,
146f10796a0Sdp-arm 	OEN_SIP_START,
147f10796a0Sdp-arm 	OEN_SIP_END,
148f10796a0Sdp-arm 	SMC_TYPE_FAST,
149f10796a0Sdp-arm 	arm_sip_setup,
150f10796a0Sdp-arm 	arm_sip_handler
151f10796a0Sdp-arm );
152