xref: /rk3399_ARM-atf/plat/arm/common/arm_sip_svc.c (revision 76a21174d2a2f1750b13b2680461b6b80a654848)
1f10796a0Sdp-arm /*
2*76a21174SMikael Olsson  * Copyright (c) 2016-2019,2021, 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>
11*76a21174SMikael 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 
37f10796a0Sdp-arm 	return 0;
38f10796a0Sdp-arm }
39f10796a0Sdp-arm 
40f10796a0Sdp-arm /*
41f10796a0Sdp-arm  * This function handles ARM defined SiP Calls
42f10796a0Sdp-arm  */
43f10796a0Sdp-arm static uintptr_t arm_sip_handler(unsigned int smc_fid,
44f10796a0Sdp-arm 			u_register_t x1,
45f10796a0Sdp-arm 			u_register_t x2,
46f10796a0Sdp-arm 			u_register_t x3,
47f10796a0Sdp-arm 			u_register_t x4,
48f10796a0Sdp-arm 			void *cookie,
49f10796a0Sdp-arm 			void *handle,
50f10796a0Sdp-arm 			u_register_t flags)
51f10796a0Sdp-arm {
52b10d4499SJeenu Viswambharan 	int call_count = 0;
53b10d4499SJeenu Viswambharan 
54*76a21174SMikael Olsson #if ENABLE_PMF
55*76a21174SMikael Olsson 
56f10796a0Sdp-arm 	/*
57f10796a0Sdp-arm 	 * Dispatch PMF calls to PMF SMC handler and return its return
58f10796a0Sdp-arm 	 * value
59f10796a0Sdp-arm 	 */
60f10796a0Sdp-arm 	if (is_pmf_fid(smc_fid)) {
61f10796a0Sdp-arm 		return pmf_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
62f10796a0Sdp-arm 				handle, flags);
63f10796a0Sdp-arm 	}
64f10796a0Sdp-arm 
65*76a21174SMikael Olsson #endif /* ENABLE_PMF */
66*76a21174SMikael Olsson 
67992f091bSAmbroise Vincent #if USE_DEBUGFS
68992f091bSAmbroise Vincent 
69992f091bSAmbroise Vincent 	if (is_debugfs_fid(smc_fid)) {
70992f091bSAmbroise Vincent 		return debugfs_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
71992f091bSAmbroise Vincent 					   handle, flags);
72992f091bSAmbroise Vincent 	}
73992f091bSAmbroise Vincent 
74992f091bSAmbroise Vincent #endif /* USE_DEBUGFS */
75992f091bSAmbroise Vincent 
76*76a21174SMikael Olsson #if ARM_ETHOSN_NPU_DRIVER
77*76a21174SMikael Olsson 
78*76a21174SMikael Olsson 	if (is_ethosn_fid(smc_fid)) {
79*76a21174SMikael Olsson 		return ethosn_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
80*76a21174SMikael Olsson 					  handle, flags);
81*76a21174SMikael Olsson 	}
82*76a21174SMikael Olsson 
83*76a21174SMikael Olsson #endif /* ARM_ETHOSN_NPU_DRIVER */
84*76a21174SMikael Olsson 
85f10796a0Sdp-arm 	switch (smc_fid) {
86b10d4499SJeenu Viswambharan 	case ARM_SIP_SVC_EXE_STATE_SWITCH: {
879d725191SBence Szépkúti 		/* Execution state can be switched only if EL3 is AArch64 */
889d725191SBence Szépkúti #ifdef __aarch64__
89b10d4499SJeenu Viswambharan 		/* Allow calls from non-secure only */
90b10d4499SJeenu Viswambharan 		if (!is_caller_non_secure(flags))
91b10d4499SJeenu Viswambharan 			SMC_RET1(handle, STATE_SW_E_DENIED);
92b10d4499SJeenu Viswambharan 
93f10796a0Sdp-arm 		/*
94b10d4499SJeenu Viswambharan 		 * Pointers used in execution state switch are all 32 bits wide
95f10796a0Sdp-arm 		 */
96b634fa91SJeenu Viswambharan 		return (uintptr_t) arm_execution_state_switch(smc_fid,
97b634fa91SJeenu Viswambharan 				(uint32_t) x1, (uint32_t) x2, (uint32_t) x3,
98b634fa91SJeenu Viswambharan 				(uint32_t) x4, handle);
999d725191SBence Szépkúti #else
1009d725191SBence Szépkúti 		/* State switch denied */
1019d725191SBence Szépkúti 		SMC_RET1(handle, STATE_SW_E_DENIED);
1029d725191SBence Szépkúti #endif /* __aarch64__ */
103b10d4499SJeenu Viswambharan 		}
104b10d4499SJeenu Viswambharan 
105b10d4499SJeenu Viswambharan 	case ARM_SIP_SVC_CALL_COUNT:
106b10d4499SJeenu Viswambharan 		/* PMF calls */
107b10d4499SJeenu Viswambharan 		call_count += PMF_NUM_SMC_CALLS;
108b10d4499SJeenu Viswambharan 
109*76a21174SMikael Olsson #if ARM_ETHOSN_NPU_DRIVER
110*76a21174SMikael Olsson 		/* ETHOSN calls */
111*76a21174SMikael Olsson 		call_count += ETHOSN_NUM_SMC_CALLS;
112*76a21174SMikael Olsson #endif /* ARM_ETHOSN_NPU_DRIVER */
113*76a21174SMikael Olsson 
114b10d4499SJeenu Viswambharan 		/* State switch call */
115b10d4499SJeenu Viswambharan 		call_count += 1;
116b10d4499SJeenu Viswambharan 
117b10d4499SJeenu Viswambharan 		SMC_RET1(handle, call_count);
118f10796a0Sdp-arm 
119f10796a0Sdp-arm 	case ARM_SIP_SVC_UID:
120f10796a0Sdp-arm 		/* Return UID to the caller */
121f10796a0Sdp-arm 		SMC_UUID_RET(handle, arm_sip_svc_uid);
122f10796a0Sdp-arm 
123f10796a0Sdp-arm 	case ARM_SIP_SVC_VERSION:
124f10796a0Sdp-arm 		/* Return the version of current implementation */
125f10796a0Sdp-arm 		SMC_RET2(handle, ARM_SIP_SVC_VERSION_MAJOR, ARM_SIP_SVC_VERSION_MINOR);
126f10796a0Sdp-arm 
127f10796a0Sdp-arm 	default:
128f10796a0Sdp-arm 		WARN("Unimplemented ARM SiP Service Call: 0x%x \n", smc_fid);
129f10796a0Sdp-arm 		SMC_RET1(handle, SMC_UNK);
130f10796a0Sdp-arm 	}
131f10796a0Sdp-arm 
132f10796a0Sdp-arm }
133f10796a0Sdp-arm 
134f10796a0Sdp-arm 
135f10796a0Sdp-arm /* Define a runtime service descriptor for fast SMC calls */
136f10796a0Sdp-arm DECLARE_RT_SVC(
137f10796a0Sdp-arm 	arm_sip_svc,
138f10796a0Sdp-arm 	OEN_SIP_START,
139f10796a0Sdp-arm 	OEN_SIP_END,
140f10796a0Sdp-arm 	SMC_TYPE_FAST,
141f10796a0Sdp-arm 	arm_sip_setup,
142f10796a0Sdp-arm 	arm_sip_handler
143f10796a0Sdp-arm );
144