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 bool lfa_is_prime_complete(uint32_t component_id) 214 { 215 if (component_id >= lfa_component_count) { 216 return false; 217 } 218 219 return (current_activation.component_id == component_id && 220 current_activation.prime_status == PRIME_COMPLETE && 221 lfa_components[component_id].activation_pending == true); 222 } 223 224 int lfa_setup(void) 225 { 226 is_lfa_initialized = lfa_initialize_components(); 227 if (!is_lfa_initialized) { 228 return -1; 229 } 230 231 lfa_reset_activation(); 232 233 return 0; 234 } 235 236 uint64_t lfa_smc_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, 237 u_register_t x3, u_register_t x4, void *cookie, 238 void *handle, u_register_t flags) 239 { 240 uint64_t retx1, retx2; 241 uint64_t lfa_flags; 242 uint8_t *uuid_p; 243 uint32_t fw_seq_id = (uint32_t)x1; 244 int ret; 245 246 /** 247 * TODO: Acquire serialization lock. 248 */ 249 250 if (!is_lfa_initialized) { 251 return LFA_NOT_SUPPORTED; 252 } 253 254 switch (smc_fid) { 255 case LFA_VERSION: 256 SMC_RET1(handle, LFA_VERSION_VAL); 257 break; 258 259 case LFA_FEATURES: 260 SMC_RET1(handle, is_lfa_fid(x1) ? LFA_SUCCESS : LFA_NOT_SUPPORTED); 261 break; 262 263 case LFA_GET_INFO: 264 /** 265 * The current specification limits this input parameter to be zero for 266 * version 1.0 of LFA 267 */ 268 if (x1 == 0ULL) { 269 SMC_RET3(handle, LFA_SUCCESS, lfa_component_count, 0); 270 } else { 271 SMC_RET1(handle, LFA_INVALID_PARAMETERS); 272 } 273 break; 274 275 case LFA_GET_INVENTORY: 276 if (lfa_component_count == 0U) { 277 SMC_RET1(handle, LFA_WRONG_STATE); 278 } 279 280 /* 281 * Check if fw_seq_id is in range. LFA_GET_INFO must be called first to scan 282 * platform firmware and create a valid number of firmware components. 283 */ 284 if (fw_seq_id >= lfa_component_count) { 285 SMC_RET1(handle, LFA_INVALID_PARAMETERS); 286 } 287 288 /* 289 * grab the UUID of asked fw_seq_id and set the return UUID 290 * variables 291 */ 292 uuid_p = (uint8_t *)&lfa_components[fw_seq_id].uuid; 293 memcpy(&retx1, uuid_p, sizeof(uint64_t)); 294 memcpy(&retx2, uuid_p + sizeof(uint64_t), sizeof(uint64_t)); 295 296 /* 297 * check the given fw_seq_id's update available 298 * and accordingly set the active_pending flag 299 */ 300 lfa_components[fw_seq_id].activation_pending = 301 is_plat_lfa_activation_pending(fw_seq_id); 302 303 INFO("Component %lu %s live activation:\n", x1, 304 lfa_components[fw_seq_id].activator ? "supports" : 305 "does not support"); 306 307 if (lfa_components[fw_seq_id].activator != NULL) { 308 INFO("Activation pending: %s\n", 309 lfa_components[fw_seq_id].activation_pending ? "true" : "false"); 310 } 311 312 INFO("x1 = 0x%016lx, x2 = 0x%016lx\n", retx1, retx2); 313 314 SMC_RET4(handle, LFA_SUCCESS, retx1, retx2, get_fw_activation_flags(fw_seq_id)); 315 316 break; 317 318 case LFA_PRIME: 319 ret = lfa_prime(x1, &lfa_flags); 320 if (ret != LFA_SUCCESS) { 321 SMC_RET1(handle, ret); 322 } else { 323 SMC_RET2(handle, ret, lfa_flags); 324 } 325 break; 326 327 case LFA_ACTIVATE: 328 ret = lfa_activate(fw_seq_id, x2, x3, x4); 329 /* TODO: implement activate again */ 330 SMC_RET2(handle, ret, 0ULL); 331 332 break; 333 334 case LFA_CANCEL: 335 ret = lfa_cancel(x1); 336 SMC_RET1(handle, ret); 337 break; 338 339 default: 340 WARN("Unimplemented LFA Service Call: 0x%x\n", smc_fid); 341 SMC_RET1(handle, SMC_UNK); 342 break; /* unreachable */ 343 344 } 345 346 SMC_RET1(handle, SMC_UNK); 347 348 return 0; 349 } 350