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