From 7a5b0839b5b95b21c82cd0a5d10dc13901e53185 Mon Sep 17 00:00:00 2001 From: John Powell Date: Wed, 20 May 2026 19:18:05 -0500 Subject: feat(lfa): serialize activate across CPUs and manage rendezvous policy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Track activation state across CPUs so skip-rendezvous activations don’t block or interleave. Split activate into prepare/finish to latch the rendezvous policy, notify the platform once per activation, and only clear activation_pending after all callers finish. Return LFA_BUSY for conflicting skip requests and LFA_CALL_AGAIN for multi‑stage activations. Change-Id: Id5a71fb6db12fe240e2bc1c9000606df935374b2 Signed-off-by: Manish V Badarkhe Signed-off-by: John Powell --- services/std_svc/lfa/lfa_main.c | 262 ++++++++++++++++++++++++++++++++-------- 1 file changed, 209 insertions(+), 53 deletions(-) diff --git a/services/std_svc/lfa/lfa_main.c b/services/std_svc/lfa/lfa_main.c index 0022e0331..2f76756bc 100644 --- a/services/std_svc/lfa/lfa_main.c +++ b/services/std_svc/lfa/lfa_main.c @@ -15,10 +15,22 @@ #include #include -static uint32_t lfa_component_count; static plat_lfa_component_info_t *lfa_components; static struct lfa_component_status current_activation; -static bool is_lfa_initialized; + +/* + * Multiple cores call into LFA service which shares these variables, so ensure + * that any accesses to these variables go back to memory. These are sort of + * "lazy" state variables where timing isn't really critical so we aren't + * explicitly using mutex for access control but we just want to make sure + * nothing is optimized away. + */ +static volatile uint32_t lfa_component_count; +static volatile bool is_lfa_initialized; +static volatile bool activation_in_progress; +static volatile bool activation_skip_cpu_rendezvous; +static volatile uint32_t activation_users; +static volatile bool activation_failed; /* * Spinlock to serialize LFA operations (PRIME, ACTIVATE). @@ -32,6 +44,11 @@ void lfa_reset_activation(void) { current_activation.component_id = LFA_INVALID_COMPONENT; current_activation.prime_status = PRIME_NONE; + current_activation.cpu_rendezvous = false; + activation_in_progress = false; + activation_skip_cpu_rendezvous = false; + activation_users = 0U; + activation_failed = false; } static int convert_to_lfa_error(int ret) @@ -114,54 +131,6 @@ static int lfa_cancel(uint32_t component_id) return ret; } -static int lfa_activate(uint32_t component_id, uint64_t flags, - uint64_t ep_address, uint64_t context_id) -{ - int ret = LFA_ACTIVATION_FAILED; - struct lfa_component_ops *activator; - - if ((lfa_component_count == 0U) || - (!lfa_components[component_id].activation_pending) || - (current_activation.prime_status != PRIME_COMPLETE)) { - return LFA_COMPONENT_WRONG_STATE; - } - - if ((component_id >= lfa_component_count) || - (current_activation.component_id != component_id)) { - return LFA_INVALID_PARAMETERS; - } - - activator = lfa_components[component_id].activator; - if (activator == NULL) { - return LFA_NOT_SUPPORTED; - } - - ret = plat_lfa_notify_activate(component_id); - if (ret != 0) { - return LFA_ACTIVATION_FAILED; - } - - /* If caller requests it, see if we can skip CPU rendezvous. */ - if (flags & LFA_SKIP_CPU_RENDEZVOUS_BIT) { - if (!activator->cpu_rendezvous_required) { - INFO("Caller requested skip CPU rendezvous.\n"); - current_activation.cpu_rendezvous = false; - } else { - ERROR("CPU rendezvous cannot be skipped!\n"); - return LFA_INVALID_PARAMETERS; - } - } - - ret = activator->activate(¤t_activation, ep_address, context_id); - - /* Clear pending flag on successful activation. */ - if (ret == LFA_SUCCESS) { - lfa_components[component_id].activation_pending = false; - } - - return ret; -} - static int lfa_prime(uint32_t component_id, uint64_t *flags) { int ret = LFA_SUCCESS; @@ -261,6 +230,110 @@ int lfa_setup(void) return 0; } +static int lfa_activate_prepare(uint32_t component_id, uint64_t flags, + struct lfa_component_ops **activator) +{ + int ret = LFA_ACTIVATION_FAILED; + /* First caller decides activation policy and performs platform notify. */ + bool first_cpu = !activation_in_progress; + + /* Check if fw_seq_id is in range. */ + if ((component_id >= lfa_component_count) || + (current_activation.component_id != component_id)) { + return LFA_INVALID_PARAMETERS; + } + + if ((lfa_component_count == 0U) || + (!lfa_components[component_id].activation_pending) || + (current_activation.prime_status != PRIME_COMPLETE)) { + return LFA_COMPONENT_WRONG_STATE; + } + + if (lfa_components[component_id].activator == NULL) { + return LFA_NOT_SUPPORTED; + } + + *activator = lfa_components[component_id].activator; + + if (first_cpu) { + activation_failed = false; + + /* + * If rendezvous is optional for this component, default to + * rendezvous unless caller explicitly asks to skip it. + * This choice is latched for the whole activation. + */ + if (!(*activator)->cpu_rendezvous_required && + ((flags & LFA_SKIP_CPU_RENDEZVOUS_BIT) == 0U)) { + current_activation.cpu_rendezvous = true; + } else { + current_activation.cpu_rendezvous = + (*activator)->cpu_rendezvous_required; + } + activation_skip_cpu_rendezvous = false; + } + /* + * Pass skip_cpu_rendezvous (flag[0]) only if flag[0]==1 + * & CPU_RENDEZVOUS is not required. + */ + if (flags & LFA_SKIP_CPU_RENDEZVOUS_BIT) { + if (!(*activator)->cpu_rendezvous_required) { + /* + * Late skip request is rejected if activation already + * started without skip. + */ + if (!first_cpu && !activation_skip_cpu_rendezvous) { + return LFA_BUSY; + } + INFO("Skipping rendezvous requested by caller.\n"); + current_activation.cpu_rendezvous = false; + activation_skip_cpu_rendezvous = true; + } + /* + * Return error if caller tries to skip rendezvous when + * it is required. + */ + else { + ERROR("CPU Rendezvous is required, can't skip.\n"); + return LFA_INVALID_PARAMETERS; + } + } + + if (first_cpu) { + /* Notify platform once per activation round. */ + ret = plat_lfa_notify_activate(component_id); + if (ret != 0) { + return LFA_ACTIVATION_FAILED; + } + activation_in_progress = true; + } + + /* Track how many CPUs have entered LFA_ACTIVATE for this round. */ + activation_users += 1U; + + return LFA_SUCCESS; +} + + +static void lfa_activate_finish(uint32_t component_id, bool activation_complete) +{ + if (activation_users > 0U) { + activation_users -= 1U; + } + + if (!activation_complete) { + return; + } + + if (activation_users == 0U) { + activation_in_progress = false; + activation_skip_cpu_rendezvous = false; + if (!activation_failed) { + lfa_components[component_id].activation_pending = false; + } + } +} + uint64_t lfa_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) @@ -362,9 +435,92 @@ uint64_t lfa_smc_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, break; case LFA_ACTIVATE: - ret = lfa_activate(fw_seq_id, x2, x3, x4); - /* TODO: implement activate again */ - SMC_RET2(handle, ret, 0ULL); + /* LFA_ACTIVATE flow: + * - LFA_SKIP_CPU_RENDEZVOUS_BIT controls skip request on entry. + * - Serialize with lfa_lock, but never block behind a + * skip-rendezvous activation. + * - lfa_activate_prepare() validates state, selects activator, + * and latches policy. + * - The first CPU decides rendezvous policy for the whole + * activation. + * - Skip rendezvous keeps the lock for the full activation; + * normal path releases it. + * - activator->activate() may return -EAGAIN, mapped to + * LFA_CALL_AGAIN in lfa_flags. + * - lfa_activate_finish() updates bookkeeping and clears + * in-progress state when done. + */ + bool hold_lock = false; + bool activation_complete = true; + struct lfa_component_ops *activator; + /* Caller request: skip CPU rendezvous for this activation. */ + bool skip_cpu_rendezvous = + ((x2 & LFA_SKIP_CPU_RENDEZVOUS_BIT) != 0U); + + /* + * When skip_cpu_rendezvous=1 (or when a skip rendezvous + * activation is already in progress), concurrent LFA_ACTIVATE + * calls must not be accepted; return LFA_BUSY instead of + * blocking. + */ + if (skip_cpu_rendezvous) { + /* Non-blocking serialization for skip rendezvous requests. */ + if (!spin_trylock(&lfa_lock)) { + SMC_RET1(handle, LFA_BUSY); + } + } else { + /* + * Normal path: take the lock, but reject if a skip + * activation is in progress. + */ + spin_lock(&lfa_lock); + if (activation_skip_cpu_rendezvous) { + spin_unlock(&lfa_lock); + SMC_RET1(handle, LFA_BUSY); + } + } + + ret = lfa_activate_prepare(fw_seq_id, x2, &activator); + hold_lock = (ret == LFA_SUCCESS) && activation_skip_cpu_rendezvous; + + /* + * Keep the lock held for the full activation only when skip + * rendezvous is in effect. + */ + if (!hold_lock) { + spin_unlock(&lfa_lock); + } + + if (ret != LFA_SUCCESS) { + SMC_RET2(handle, ret, 0ULL); + } + + if (activator->activate != NULL) { + ret = activator->activate(¤t_activation, x3, x4); + } + + /* Reacquire lock before updating shared activation state. */ + if (!hold_lock) { + spin_lock(&lfa_lock); + } + + lfa_flags = 0ULL; + if (ret == -EAGAIN) { + /* Multi-stage activation: caller must reissue LFA_ACTIVATE. */ + ret = LFA_SUCCESS; + lfa_flags = LFA_CALL_AGAIN; + activation_complete = false; + } else if (ret != LFA_SUCCESS) { + activation_failed = true; + } + + /* Update activation bookkeeping and clear in-progress state if complete. */ + lfa_activate_finish(fw_seq_id, activation_complete); + + spin_unlock(&lfa_lock); + + SMC_RET2(handle, ret, lfa_flags); + break; case LFA_CANCEL: -- cgit v1.2.3