xref: /rk3399_ARM-atf/plat/arm/common/arm_sip_svc.c (revision 273b898388adde0d7cc858fab6774b1da61caa0f)
1 /*
2  * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdint.h>
8 
9 #include <common/debug.h>
10 #include <common/runtime_svc.h>
11 #include <drivers/arm/ethosn.h>
12 #include <lib/debugfs.h>
13 #include <lib/pmf/pmf.h>
14 #include <plat/arm/common/arm_sip_svc.h>
15 #include <plat/arm/common/plat_arm.h>
16 #include <tools_share/uuid.h>
17 
18 /* ARM SiP Service UUID */
19 DEFINE_SVC_UUID2(arm_sip_svc_uid,
20 	0x556d75e2, 0x6033, 0xb54b, 0xb5, 0x75,
21 	0x62, 0x79, 0xfd, 0x11, 0x37, 0xff);
22 
23 static int arm_sip_setup(void)
24 {
25 	if (pmf_setup() != 0) {
26 		return 1;
27 	}
28 
29 #if USE_DEBUGFS
30 
31 	if (debugfs_smc_setup() != 0) {
32 		return 1;
33 	}
34 
35 #endif /* USE_DEBUGFS */
36 
37 #if ETHOSN_NPU_DRIVER
38 
39 	if (ethosn_smc_setup() != 0) {
40 		return 1;
41 	}
42 
43 #endif /* ETHOSN_NPU_DRIVER */
44 
45 	return 0;
46 }
47 
48 /*
49  * This function handles ARM defined SiP Calls
50  */
51 static uintptr_t arm_sip_handler(unsigned int smc_fid,
52 			u_register_t x1,
53 			u_register_t x2,
54 			u_register_t x3,
55 			u_register_t x4,
56 			void *cookie,
57 			void *handle,
58 			u_register_t flags)
59 {
60 	int call_count = 0;
61 
62 #if ENABLE_PMF
63 
64 	/*
65 	 * Dispatch PMF calls to PMF SMC handler and return its return
66 	 * value
67 	 */
68 	if (is_pmf_fid(smc_fid)) {
69 		return pmf_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
70 				handle, flags);
71 	}
72 
73 #endif /* ENABLE_PMF */
74 
75 #if USE_DEBUGFS
76 	if (is_debugfs_fid_deprecated(smc_fid)) {
77 		NOTICE("Debugfs Interface usage from arm-sip range is deprecated. \
78 			Please migrate smc call to vendor-specific el3 range.\n");
79 		return debugfs_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
80 					   handle, flags);
81 	}
82 
83 #endif /* USE_DEBUGFS */
84 
85 #if ETHOSN_NPU_DRIVER
86 
87 	if (is_ethosn_fid(smc_fid)) {
88 		return ethosn_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
89 					  handle, flags);
90 	}
91 
92 #endif /* ETHOSN_NPU_DRIVER */
93 
94 	switch (smc_fid) {
95 	case ARM_SIP_SVC_EXE_STATE_SWITCH: {
96 		/* Execution state can be switched only if EL3 is AArch64 */
97 #ifdef __aarch64__
98 		/* Allow calls from non-secure only */
99 		if (!is_caller_non_secure(flags))
100 			SMC_RET1(handle, STATE_SW_E_DENIED);
101 
102 		/*
103 		 * Pointers used in execution state switch are all 32 bits wide
104 		 */
105 		return (uintptr_t) arm_execution_state_switch(smc_fid,
106 				(uint32_t) x1, (uint32_t) x2, (uint32_t) x3,
107 				(uint32_t) x4, handle);
108 #else
109 		/* State switch denied */
110 		SMC_RET1(handle, STATE_SW_E_DENIED);
111 #endif /* __aarch64__ */
112 		}
113 
114 	case ARM_SIP_SVC_CALL_COUNT:
115 		/* PMF calls */
116 		call_count += PMF_NUM_SMC_CALLS;
117 
118 #if ETHOSN_NPU_DRIVER
119 		/* ETHOSN calls */
120 		call_count += ETHOSN_NUM_SMC_CALLS;
121 #endif          /* ETHOSN_NPU_DRIVER */
122 
123 		/* State switch call */
124 		call_count += 1;
125 
126 		SMC_RET1(handle, call_count);
127 
128 	case ARM_SIP_SVC_UID:
129 		/* Return UID to the caller */
130 		SMC_UUID_RET(handle, arm_sip_svc_uid);
131 
132 	case ARM_SIP_SVC_VERSION:
133 		/* Return the version of current implementation */
134 		SMC_RET2(handle, ARM_SIP_SVC_VERSION_MAJOR, ARM_SIP_SVC_VERSION_MINOR);
135 
136 	default:
137 		break;
138 	}
139 
140 	/*
141 	 * Fall back to allow Arm platform specific handler.
142 	 * TODO: Refactor needed to move out generic handlers from this file and
143 	 * only keep Arm Platform specific handlers here.
144 	 */
145 	return plat_arm_sip_handler(smc_fid, x1, x2, x3, x4,
146 					cookie, handle, flags);
147 }
148 
149 
150 /* Define a runtime service descriptor for fast SMC calls */
151 DECLARE_RT_SVC(
152 	arm_sip_svc,
153 	OEN_SIP_START,
154 	OEN_SIP_END,
155 	SMC_TYPE_FAST,
156 	arm_sip_setup,
157 	arm_sip_handler
158 );
159