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