1 /* 2 * Copyright (c) 2025, Arm Limited. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <errno.h> 8 #include <string.h> 9 10 #include <plat/common/platform.h> 11 #include <services/bl31_lfa.h> 12 #include <services/lfa_svc.h> 13 #include <services/rmmd_rmm_lfa.h> 14 #include <smccc_helpers.h> 15 16 static uint32_t lfa_component_count; 17 static plat_lfa_component_info_t *lfa_components; 18 static struct lfa_component_status current_activation; 19 static bool is_lfa_initialized; 20 21 void lfa_reset_activation(void) 22 { 23 current_activation.component_id = LFA_INVALID_COMPONENT; 24 current_activation.prime_status = PRIME_NONE; 25 current_activation.cpu_rendezvous_required = false; 26 } 27 28 static int convert_to_lfa_error(int ret) 29 { 30 switch (ret) { 31 case 0: 32 return LFA_SUCCESS; 33 case -EAUTH: 34 return LFA_AUTH_ERROR; 35 case -ENOMEM: 36 return LFA_NO_MEMORY; 37 default: 38 return LFA_DEVICE_ERROR; 39 } 40 } 41 42 static bool lfa_initialize_components(void) 43 { 44 lfa_component_count = plat_lfa_get_components(&lfa_components); 45 46 if (lfa_component_count == 0U || lfa_components == NULL) { 47 /* unlikely to reach here */ 48 ERROR("Invalid LFA component setup: count = 0 or components are NULL"); 49 return false; 50 } 51 52 return true; 53 } 54 55 static uint64_t get_fw_activation_flags(uint32_t fw_seq_id) 56 { 57 const plat_lfa_component_info_t *comp = 58 &lfa_components[fw_seq_id]; 59 uint64_t flags = 0ULL; 60 61 flags |= ((comp->activator == NULL ? 0ULL : 1ULL) 62 << LFA_ACTIVATION_CAPABLE_SHIFT); 63 flags |= (uint64_t)(comp->activation_pending) 64 << LFA_ACTIVATION_PENDING_SHIFT; 65 66 if (comp->activator != NULL) { 67 flags |= ((comp->activator->may_reset_cpu ? 1ULL : 0ULL) 68 << LFA_MAY_RESET_CPU_SHIFT); 69 flags |= ((comp->activator->cpu_rendezvous_required ? 0ULL : 1ULL) 70 << LFA_CPU_RENDEZVOUS_OPTIONAL_SHIFT); 71 } 72 73 return flags; 74 } 75 76 static int lfa_cancel(uint32_t component_id) 77 { 78 int ret = LFA_SUCCESS; 79 80 if (lfa_component_count == 0U) { 81 return LFA_WRONG_STATE; 82 } 83 84 /* Check if component ID is in range. */ 85 if ((component_id >= lfa_component_count) || 86 (component_id != current_activation.component_id)) { 87 return LFA_INVALID_PARAMETERS; 88 } 89 90 ret = plat_lfa_cancel(component_id); 91 if (ret != LFA_SUCCESS) { 92 return LFA_BUSY; 93 } 94 95 /* TODO: add proper termination prime and activate phases */ 96 lfa_reset_activation(); 97 98 return ret; 99 } 100 101 static int lfa_activate(uint32_t component_id, uint64_t flags, 102 uint64_t ep_address, uint64_t context_id) 103 { 104 int ret = LFA_ACTIVATION_FAILED; 105 struct lfa_component_ops *activator; 106 107 if ((lfa_component_count == 0U) || 108 (!lfa_components[component_id].activation_pending) || 109 (current_activation.prime_status != PRIME_COMPLETE)) { 110 return LFA_COMPONENT_WRONG_STATE; 111 } 112 113 /* Check if fw_seq_id is in range. */ 114 if ((component_id >= lfa_component_count) || 115 (current_activation.component_id != component_id)) { 116 return LFA_INVALID_PARAMETERS; 117 } 118 119 if (lfa_components[component_id].activator == NULL) { 120 return LFA_NOT_SUPPORTED; 121 } 122 123 activator = lfa_components[component_id].activator; 124 if (activator->activate != NULL) { 125 /* 126 * Pass skip_cpu_rendezvous (flag[0]) only if flag[0]==1 127 * & CPU_RENDEZVOUS is not required. 128 */ 129 if (flags & LFA_SKIP_CPU_RENDEZVOUS_BIT) { 130 if (!activator->cpu_rendezvous_required) { 131 INFO("Skipping rendezvous requested by caller.\n"); 132 current_activation.cpu_rendezvous_required = false; 133 } 134 /* 135 * Return error if caller tries to skip rendezvous when 136 * it is required. 137 */ 138 else { 139 ERROR("CPU Rendezvous is required, can't skip.\n"); 140 return LFA_INVALID_PARAMETERS; 141 } 142 } 143 144 ret = activator->activate(¤t_activation, ep_address, 145 context_id); 146 } 147 148 lfa_components[component_id].activation_pending = false; 149 150 return ret; 151 } 152 153 static int lfa_prime(uint32_t component_id, uint64_t *flags) 154 { 155 int ret = LFA_SUCCESS; 156 struct lfa_component_ops *activator; 157 158 if (lfa_component_count == 0U || 159 !lfa_components[component_id].activation_pending) { 160 return LFA_WRONG_STATE; 161 } 162 163 /* Check if fw_seq_id is in range. */ 164 if (component_id >= lfa_component_count) { 165 return LFA_INVALID_PARAMETERS; 166 } 167 168 if (lfa_components[component_id].activator == NULL) { 169 return LFA_NOT_SUPPORTED; 170 } 171 172 switch (current_activation.prime_status) { 173 case PRIME_NONE: 174 current_activation.component_id = component_id; 175 current_activation.prime_status = PRIME_STARTED; 176 break; 177 178 case PRIME_STARTED: 179 if (current_activation.component_id != component_id) { 180 /* Mismatched component trying to continue PRIME - error */ 181 return LFA_WRONG_STATE; 182 } 183 break; 184 185 case PRIME_COMPLETE: 186 default: 187 break; 188 } 189 190 ret = plat_lfa_load_auth_image(component_id); 191 ret = convert_to_lfa_error(ret); 192 193 activator = lfa_components[component_id].activator; 194 if (activator->prime != NULL) { 195 ret = activator->prime(¤t_activation); 196 if (ret != LFA_SUCCESS) { 197 /* 198 * TODO: it should be LFA_PRIME_FAILED but specification 199 * has not define this error yet 200 */ 201 return ret; 202 } 203 } 204 205 current_activation.prime_status = PRIME_COMPLETE; 206 207 /* TODO: split this into multiple PRIME calls */ 208 *flags = 0ULL; 209 210 return ret; 211 } 212 213 int lfa_setup(void) 214 { 215 is_lfa_initialized = lfa_initialize_components(); 216 if (!is_lfa_initialized) { 217 return -1; 218 } 219 220 lfa_reset_activation(); 221 222 return 0; 223 } 224 225 uint64_t lfa_smc_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, 226 u_register_t x3, u_register_t x4, void *cookie, 227 void *handle, u_register_t flags) 228 { 229 uint64_t retx1, retx2; 230 uint64_t lfa_flags; 231 uint8_t *uuid_p; 232 uint32_t fw_seq_id = (uint32_t)x1; 233 int ret; 234 235 /** 236 * TODO: Acquire serialization lock. 237 */ 238 239 if (!is_lfa_initialized) { 240 return LFA_NOT_SUPPORTED; 241 } 242 243 switch (smc_fid) { 244 case LFA_VERSION: 245 SMC_RET1(handle, LFA_VERSION_VAL); 246 break; 247 248 case LFA_FEATURES: 249 SMC_RET1(handle, is_lfa_fid(x1) ? LFA_SUCCESS : LFA_NOT_SUPPORTED); 250 break; 251 252 case LFA_GET_INFO: 253 /** 254 * The current specification limits this input parameter to be zero for 255 * version 1.0 of LFA 256 */ 257 if (x1 == 0ULL) { 258 SMC_RET3(handle, LFA_SUCCESS, lfa_component_count, 0); 259 } else { 260 SMC_RET1(handle, LFA_INVALID_PARAMETERS); 261 } 262 break; 263 264 case LFA_GET_INVENTORY: 265 if (lfa_component_count == 0U) { 266 SMC_RET1(handle, LFA_WRONG_STATE); 267 } 268 269 /* 270 * Check if fw_seq_id is in range. LFA_GET_INFO must be called first to scan 271 * platform firmware and create a valid number of firmware components. 272 */ 273 if (fw_seq_id >= lfa_component_count) { 274 SMC_RET1(handle, LFA_INVALID_PARAMETERS); 275 } 276 277 /* 278 * grab the UUID of asked fw_seq_id and set the return UUID 279 * variables 280 */ 281 uuid_p = (uint8_t *)&lfa_components[fw_seq_id].uuid; 282 memcpy(&retx1, uuid_p, sizeof(uint64_t)); 283 memcpy(&retx2, uuid_p + sizeof(uint64_t), sizeof(uint64_t)); 284 285 /* 286 * check the given fw_seq_id's update available 287 * and accordingly set the active_pending flag 288 */ 289 lfa_components[fw_seq_id].activation_pending = 290 is_plat_lfa_activation_pending(fw_seq_id); 291 292 INFO("Component %lu %s live activation:\n", x1, 293 lfa_components[fw_seq_id].activator ? "supports" : 294 "does not support"); 295 296 if (lfa_components[fw_seq_id].activator != NULL) { 297 INFO("Activation pending: %s\n", 298 lfa_components[fw_seq_id].activation_pending ? "true" : "false"); 299 } 300 301 INFO("x1 = 0x%016lx, x2 = 0x%016lx\n", retx1, retx2); 302 303 SMC_RET4(handle, LFA_SUCCESS, retx1, retx2, get_fw_activation_flags(fw_seq_id)); 304 305 break; 306 307 case LFA_PRIME: 308 ret = lfa_prime(x1, &lfa_flags); 309 if (ret != LFA_SUCCESS) { 310 SMC_RET1(handle, ret); 311 } else { 312 SMC_RET2(handle, ret, lfa_flags); 313 } 314 break; 315 316 case LFA_ACTIVATE: 317 ret = lfa_activate(fw_seq_id, x2, x3, x4); 318 /* TODO: implement activate again */ 319 SMC_RET2(handle, ret, 0ULL); 320 321 break; 322 323 case LFA_CANCEL: 324 ret = lfa_cancel(x1); 325 SMC_RET1(handle, ret); 326 break; 327 328 default: 329 WARN("Unimplemented LFA Service Call: 0x%x\n", smc_fid); 330 SMC_RET1(handle, SMC_UNK); 331 break; /* unreachable */ 332 333 } 334 335 SMC_RET1(handle, SMC_UNK); 336 337 return 0; 338 } 339