1 /*
2  * Copyright (c) 2014-2022, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdint.h>
9 
10 #include <common/debug.h>
11 #include <common/runtime_svc.h>
12 #include <lib/el3_runtime/cpu_data.h>
13 #include <lib/pmf/pmf.h>
14 #include <lib/psci/psci.h>
15 #include <lib/runtime_instr.h>
16 #include <services/drtm_svc.h>
17 #include <services/pci_svc.h>
18 #include <services/rmmd_svc.h>
19 #include <services/sdei.h>
20 #include <services/spm_mm_svc.h>
21 #include <services/spmc_svc.h>
22 #include <services/spmd_svc.h>
23 #include <services/std_svc.h>
24 #include <services/trng_svc.h>
25 #include <smccc_helpers.h>
26 #include <tools_share/uuid.h>
27 
28 /* Standard Service UUID */
29 static uuid_t arm_svc_uid = {
30 	{0x5b, 0x90, 0x8d, 0x10},
31 	{0x63, 0xf8},
32 	{0xe8, 0x47},
33 	0xae, 0x2d,
34 	{0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2}
35 };
36 
37 /* Setup Standard Services */
std_svc_setup(void)38 static int32_t std_svc_setup(void)
39 {
40 	uintptr_t svc_arg;
41 	int ret = 0;
42 
43 	svc_arg = get_arm_std_svc_args(PSCI_FID_MASK);
44 	assert(svc_arg);
45 
46 	/*
47 	 * PSCI is one of the specifications implemented as a Standard Service.
48 	 * The `psci_setup()` also does EL3 architectural setup.
49 	 */
50 	if (psci_setup((const psci_lib_args_t *)svc_arg) != PSCI_E_SUCCESS) {
51 		ret = 1;
52 	}
53 
54 #if SPM_MM
55 	if (spm_mm_setup() != 0) {
56 		ret = 1;
57 	}
58 #endif
59 
60 #if defined(SPD_spmd)
61 	if (spmd_setup() != 0) {
62 		ret = 1;
63 	}
64 #endif
65 
66 #if ENABLE_RME
67 	if (rmmd_setup() != 0) {
68 		ret = 1;
69 	}
70 #endif
71 
72 #if SDEI_SUPPORT
73 	/* SDEI initialisation */
74 	sdei_init();
75 #endif
76 
77 #if TRNG_SUPPORT
78 	/* TRNG initialisation */
79 	trng_setup();
80 #endif /* TRNG_SUPPORT */
81 
82 #if DRTM_SUPPORT
83 	if (drtm_setup() != 0) {
84 		ret = 1;
85 	}
86 #endif /* DRTM_SUPPORT */
87 
88 	return ret;
89 }
90 
91 /*
92  * Top-level Standard Service SMC handler. This handler will in turn dispatch
93  * calls to PSCI SMC handler
94  */
std_svc_smc_handler(uint32_t smc_fid,u_register_t x1,u_register_t x2,u_register_t x3,u_register_t x4,void * cookie,void * handle,u_register_t flags)95 static uintptr_t std_svc_smc_handler(uint32_t smc_fid,
96 			     u_register_t x1,
97 			     u_register_t x2,
98 			     u_register_t x3,
99 			     u_register_t x4,
100 			     void *cookie,
101 			     void *handle,
102 			     u_register_t flags)
103 {
104 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
105 		/* 32-bit SMC function, clear top parameter bits */
106 
107 		x1 &= UINT32_MAX;
108 		x2 &= UINT32_MAX;
109 		x3 &= UINT32_MAX;
110 		x4 &= UINT32_MAX;
111 	}
112 
113 	/*
114 	 * Dispatch PSCI calls to PSCI SMC handler and return its return
115 	 * value
116 	 */
117 	if (is_psci_fid(smc_fid)) {
118 		uint64_t ret;
119 
120 #if ENABLE_RUNTIME_INSTRUMENTATION
121 
122 		/*
123 		 * Flush cache line so that even if CPU power down happens
124 		 * the timestamp update is reflected in memory.
125 		 */
126 		PMF_WRITE_TIMESTAMP(rt_instr_svc,
127 		    RT_INSTR_ENTER_PSCI,
128 		    PMF_CACHE_MAINT,
129 		    get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX]));
130 #endif
131 
132 		ret = psci_smc_handler(smc_fid, x1, x2, x3, x4,
133 		    cookie, handle, flags);
134 
135 #if ENABLE_RUNTIME_INSTRUMENTATION
136 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
137 		    RT_INSTR_EXIT_PSCI,
138 		    PMF_NO_CACHE_MAINT);
139 #endif
140 
141 		SMC_RET1(handle, ret);
142 	}
143 
144 #if SPM_MM
145 	/*
146 	 * Dispatch SPM calls to SPM SMC handler and return its return
147 	 * value
148 	 */
149 	if (is_spm_mm_fid(smc_fid)) {
150 		return spm_mm_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
151 					  handle, flags);
152 	}
153 #endif
154 
155 #if defined(SPD_spmd)
156 	/*
157 	 * Dispatch FFA calls to the FFA SMC handler implemented by the SPM
158 	 * dispatcher and return its return value
159 	 */
160 	if (is_ffa_fid(smc_fid)) {
161 		return spmd_ffa_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
162 					    handle, flags);
163 	}
164 #endif
165 
166 #if SDEI_SUPPORT
167 	if (is_sdei_fid(smc_fid)) {
168 		return sdei_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
169 				flags);
170 	}
171 #endif
172 
173 #if TRNG_SUPPORT
174 	if (is_trng_fid(smc_fid)) {
175 		return trng_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
176 				flags);
177 	}
178 #endif /* TRNG_SUPPORT */
179 
180 #if ENABLE_RME
181 
182 	if (is_rmmd_el3_fid(smc_fid)) {
183 		return rmmd_rmm_el3_handler(smc_fid, x1, x2, x3, x4, cookie,
184 					    handle, flags);
185 	}
186 
187 	if (is_rmi_fid(smc_fid)) {
188 		return rmmd_rmi_handler(smc_fid, x1, x2, x3, x4, cookie,
189 					handle, flags);
190 	}
191 #endif
192 
193 #if SMC_PCI_SUPPORT
194 	if (is_pci_fid(smc_fid)) {
195 		return pci_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
196 				       flags);
197 	}
198 #endif
199 
200 #if DRTM_SUPPORT
201 	if (is_drtm_fid(smc_fid)) {
202 		return drtm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
203 					flags);
204 	}
205 #endif /* DRTM_SUPPORT */
206 
207 	switch (smc_fid) {
208 	case ARM_STD_SVC_CALL_COUNT:
209 		/*
210 		 * Return the number of Standard Service Calls. PSCI is the only
211 		 * standard service implemented; so return number of PSCI calls
212 		 */
213 		SMC_RET1(handle, PSCI_NUM_CALLS);
214 
215 	case ARM_STD_SVC_UID:
216 		/* Return UID to the caller */
217 		SMC_UUID_RET(handle, arm_svc_uid);
218 
219 	case ARM_STD_SVC_VERSION:
220 		/* Return the version of current implementation */
221 		SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR);
222 
223 	default:
224 		VERBOSE("Unimplemented Standard Service Call: 0x%x \n", smc_fid);
225 		SMC_RET1(handle, SMC_UNK);
226 	}
227 }
228 
229 /* Register Standard Service Calls as runtime service */
230 DECLARE_RT_SVC(
231 		std_svc,
232 
233 		OEN_STD_START,
234 		OEN_STD_END,
235 		SMC_TYPE_FAST,
236 		std_svc_setup,
237 		std_svc_smc_handler
238 );
239