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