diff options
Diffstat (limited to 'services/std_svc/lfa')
| -rw-r--r-- | services/std_svc/lfa/bl31_lfa.c | 373 | ||||
| -rw-r--r-- | services/std_svc/lfa/lfa.mk | 8 | ||||
| -rw-r--r-- | services/std_svc/lfa/lfa_holding_pen.c | 64 | ||||
| -rw-r--r-- | services/std_svc/lfa/lfa_main.c | 355 | ||||
| -rw-r--r-- | services/std_svc/lfa/lfa_relocatable.S | 108 | ||||
| -rw-r--r-- | services/std_svc/lfa/lfa_reset.c | 173 |
6 files changed, 969 insertions, 112 deletions
diff --git a/services/std_svc/lfa/bl31_lfa.c b/services/std_svc/lfa/bl31_lfa.c index 2a75065e1..cb6f42e0b 100644 --- a/services/std_svc/lfa/bl31_lfa.c +++ b/services/std_svc/lfa/bl31_lfa.c @@ -1,28 +1,258 @@ /* - * Copyright (c) 2025, Arm Limited. All rights reserved. + * Copyright (c) 2025-2026, Arm Limited. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ +#include <assert.h> +#include <errno.h> + +#include <common/build_message.h> +#include <common/debug.h> +#include <lib/xlat_tables/xlat_tables_v2.h> +#include <plat/common/platform.h> #include <services/bl31_lfa.h> +#include <services/lfa_holding_pen.h> #include <services/lfa_svc.h> -static int lfa_bl31_prime(struct lfa_component_status *activation) +static const uintptr_t TEXT_START = (uintptr_t)__TEXT_START__; + +/* Static function prototypes. */ +static int lfa_bl31_check(uintptr_t base_va, size_t size, uint64_t **entry); +static int lfa_r_bl31_copy(uintptr_t va, uint64_t *entry, void *src, size_t size, bool reset); + +static inline size_t lfa_bl31_errata_size(void) +{ + return ERRATA_END - ERRATA_START; +} + +static inline size_t lfa_bl31_cpu_ops_size(void) +{ + return CPU_OPS_END - CPU_OPS_START; +} + +static int lfa_bl31_patch_region(struct lfa_component_status *activation, + uintptr_t dst_va, size_t size, bool reset) +{ + uint64_t *entry = NULL; + size_t src_offset; + uintptr_t src_va; + uintptr_t patch_start; + uintptr_t patch_end; + size_t patch_size; + + patch_start = dst_va & ~(PAGE_SIZE - 1U); + patch_end = (dst_va + size + PAGE_SIZE - 1U) & ~(PAGE_SIZE - 1U); + patch_size = patch_end - patch_start; + + if ((patch_start < TEXT_START) || + ((patch_size > activation->image_size) && + (activation->image_size != 0))) { + ERROR("BL31 LFA: invalid patch region dst=0x%lx size=0x%zx image_size=0x%lx\n", + dst_va, size, activation->image_size); + return LFA_CRITICAL_ERROR; + } + + src_offset = patch_start - TEXT_START; + if ((activation->image_size != 0) && + ((src_offset > activation->image_size) || + (patch_size > (activation->image_size - src_offset)))) { + ERROR("BL31 LFA: staged image too small for patch region 0x%lx size 0x%zx " + "aligned_start 0x%lx aligned_size 0x%zx\n", + dst_va, size, patch_start, patch_size); + return LFA_CRITICAL_ERROR; + } + + src_va = activation->image_address + src_offset; + + if (lfa_bl31_check(patch_start, patch_size, &entry) != 0) { + ERROR("BL31 LFA: xlat check failed for patch region dst=0x%lx size=0x%zx " + "aligned_start 0x%lx aligned_size 0x%zx\n", + dst_va, size, patch_start, patch_size); + return LFA_CRITICAL_ERROR; + } + + lfa_r_bl31_copy(patch_start, entry, (void *)src_va, patch_size, reset); + return LFA_SUCCESS; +} + +int32_t lfa_bl31_prime(struct lfa_component_status *activation) +{ + // TODO: Check that activation->image_size is not too large. + return LFA_SUCCESS; +} + +/* Function that actually performs the activation of the firmware image */ +static int lfa_bl31_image_activation(struct lfa_component_status *activation, + uint64_t ep_address, uint64_t context_id) +{ + VERBOSE("BL31 LFA: Starting BL31 Image Activation (core %d)\n", plat_my_core_pos()); + VERBOSE(" Address: 0x%lX\n", activation->image_address); + VERBOSE(" Size: %ld bytes\n", activation->image_size); + VERBOSE(" Reset: %s\n", activation->reset ? "yes" : "no"); + +#if !ENABLE_RUNTIME_INSTRUMENTATION + /* + * Used to avoid console corruption. Not used if measuring performance + * since printouts are removed. + */ + console_flush(); +#endif + + /* Prepare for warm reset and setup NS entrypoint for primary cores */ + if (activation->reset) { + if (prepare_warm_reset(ep_address, context_id, + (uint64_t)&lfa_r_holding_lock_var) != LFA_SUCCESS) { + lfa_r_holding_unlock(&lfa_r_holding_lock_var); + return LFA_CRITICAL_ERROR; + } + } + + if (lfa_bl31_patch_region(activation, CPU_OPS_START, + lfa_bl31_cpu_ops_size(), false) != LFA_SUCCESS) { + return LFA_CRITICAL_ERROR; + } + + /* + * Patch the errata section last so reset, if requested, is triggered + * only after cpu_ops has been updated to point at the new errata code. + */ + return lfa_bl31_patch_region(activation, ERRATA_START, + lfa_bl31_errata_size(), + activation->reset); +} + +int32_t lfa_bl31_activate(struct lfa_component_status *activation, + uint64_t ep_address, uint64_t context_id) { - return LFA_WRONG_STATE; + uint32_t core_pos = plat_my_core_pos(); + int ret = LFA_SUCCESS; + + lfa_r_ep_addresses[core_pos] = ep_address; + lfa_r_context_ids[core_pos] = context_id; + + /* Case when Firmware requires CPU Rendezvous for LFA Activation */ + if (activation->cpu_rendezvous) { + /* + * Only one core will return true from this function and it + * controls the activation process. + */ + if (lfa_holding_start()) { + VERBOSE("BL31 LFA: CPU %d is leader core\n", core_pos); + + /* + * Before we release other cores from the holding pen + * try to get the image info. That way if this fails the + * system will still be in a recoverable state. + */ + ret = plat_lfa_get_image_info(activation->component_id, + &activation->image_address, + &activation->image_size); + if (ret) { + ERROR("BL31 LFA: Could not get image info!\n"); + lfa_holding_release(LFA_CRITICAL_ERROR); + return LFA_CRITICAL_ERROR; + } + + /* + * Only one core will load the relocatable code module and + * acquire the relocatable lock, then release the rest of the + * cores which will then wait for us to release the relocatable + * lock once the activation is complete. + */ + lfa_load_relocatable(); + lfa_r_holding_lock(&lfa_r_holding_lock_var); + lfa_holding_release(LFA_SUCCESS); + + /* + * Perform image activation. Will not return if reset + * requested. + */ + ret = lfa_bl31_image_activation(activation, ep_address, context_id); + + /* Unlock holding pen for all other cores */ + lfa_r_holding_unlock(&lfa_r_holding_lock_var); + + if (ret) { + ERROR("BL31 LFA: failed image activation with error: %d\n", ret); + return ret; + } + + INFO("BL31 LFA: Successful image activation!\n"); + } else { + /* Wait until release from rendezvous holding pen. */ + ret = lfa_holding_wait(); + if (ret != LFA_SUCCESS) { + return ret; + } + + if (activation->reset) { + /* + * Prepare for warm reset and setup NS + * entrypoint for secondary cores + */ + if (prepare_warm_reset(ep_address, context_id, + (uint64_t)&lfa_r_holding_lock_var) != LFA_SUCCESS) { + return LFA_CRITICAL_ERROR; + } + + /* + * Wait until activation of the image is + * complete and warm reset occurs on main core + * then reset. + */ + lfa_r_holding_wait_warm_reset(&lfa_r_holding_lock_var); + } else { + /* + * Wait until main core is completed with + * activation + */ + lfa_r_holding_wait(&lfa_r_holding_lock_var); + } + } + } + + /* + * No CPU rendezvous required. Perform LFA Activate with single core + * NOTE: Assumption that only one core will call activate with + * CPU_RENDEZVOUS=0 + */ + else { + /* + * Loading relocatable module normally happens in the holding + * pen. Since there is no holding pen without CPU rendezvous we + * are doing it here instead. + */ + lfa_load_relocatable(); + + /* Perform actual image activation */ + ret = lfa_bl31_image_activation(activation, ep_address, context_id); + if (ret) { + ERROR("Failed image activation with error: %d\n", ret); + return ret; + } + } + + NOTICE("BL31 Live Activation Without Reset %u\n", plat_my_core_pos()); + NOTICE(" Version : %s\n", build_version_string); + NOTICE(" %s\n", build_message); + + return ret; } -static int lfa_bl31_activate(struct lfa_component_status *activation, - uint64_t ep_address, - uint64_t context_id) +int32_t lfa_bl31_cancel(struct lfa_component_status *activation) { - return LFA_WRONG_STATE; + /* Let CPUs out of the holding pen if needed. */ + lfa_holding_release(LFA_ACTIVATION_FAILED); + + return LFA_SUCCESS; } -static struct lfa_component_ops bl31_activator = { +struct lfa_component_ops bl31_activator = { .prime = lfa_bl31_prime, .activate = lfa_bl31_activate, - .may_reset_cpu = false, + .cancel = lfa_bl31_cancel, + .may_reset_cpu = true, .cpu_rendezvous_required = true, }; @@ -30,3 +260,128 @@ struct lfa_component_ops *get_bl31_activator(void) { return &bl31_activator; } + +uint64_t *lfa_get_xlat_table_entry(uintptr_t virtual_addr, const xlat_ctx_t *ctx) +{ + uint64_t *table = ctx->base_table; + unsigned int entries = ctx->base_table_entries; + unsigned int start_level = GET_XLAT_TABLE_LEVEL_BASE((unsigned long long)ctx->va_max_address + 1ULL); + + for (unsigned int level = start_level; level <= XLAT_TABLE_LEVEL_MAX; level++) { + uint64_t idx, desc, desc_type; + + idx = XLAT_TABLE_IDX(virtual_addr, level); + if (idx >= entries) { + return NULL; + } + + desc = table[idx]; + desc_type = desc & DESC_MASK; + + if (desc_type == INVALID_DESC) { + return NULL; + } + + if (level == XLAT_TABLE_LEVEL_MAX) { + /* + * Only page descriptors allowed at the final lookup + * level. + */ + assert(desc_type == PAGE_DESC); + return &table[idx]; + } + + if (desc_type == BLOCK_DESC) { + return &table[idx]; + } + + assert(desc_type == TABLE_DESC); + table = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK); + entries = XLAT_TABLE_ENTRIES; + } + + return NULL; +} + +int lfa_bl31_check(uintptr_t base_va, size_t size, uint64_t **entry) +{ + const xlat_ctx_t *ctx = get_xlat_tables(); + + assert(ctx != NULL); + assert(ctx->initialized); + + if (!IS_PAGE_ALIGNED(base_va)) { + WARN("%s: Address 0x%lx is not aligned on a page boundary.\n", __func__, base_va); + return -EINVAL; + } + + if (size == 0U) { + WARN("%s: Size is 0.\n", __func__); + return -EINVAL; + } + + size_t pages_count = size / PAGE_SIZE; + + VERBOSE("BL31 update will impact %zu pages starting from address 0x%lx...\n", + pages_count, base_va); + + *entry = lfa_get_xlat_table_entry(base_va, ctx); + + uint64_t *last = lfa_get_xlat_table_entry(base_va + (pages_count - 1) * PAGE_SIZE, ctx); + + /* Check that all descriptors are in the same page. */ + if ((((uint64_t)*entry) >> 12) != ((((uint64_t)last) >> 12))) { + ERROR("All page descriptors are not part of the same page.\n"); + return -EINVAL; + } + + return 0; +} + +__attribute__((noinline, section(".lfa_relocatable_code"))) +int lfa_r_bl31_copy(uintptr_t base_va, uint64_t *entry, void *src, size_t size, bool reset) +{ + do { + uint64_t desc = *entry; + size_t count = MIN(PAGE_SIZE, size) / sizeof(uint64_t); + + *entry = INVALID_DESC; + dsbishst(); + tlbivae3is(TLBI_ADDR(base_va)); + dsbish(); + isb(); + *entry = (desc & ~LOWER_ATTRS(AP_RO)); + dsbish(); + + for (uint64_t i = 0; i < count; i++) { + ((uint64_t *) base_va)[i] = ((uint64_t *) src)[i]; + } + if (size <= PAGE_SIZE) { + for (uint64_t i = count * sizeof(uint64_t); i < size; i++) { + ((uint8_t *) base_va)[i] = ((uint8_t *) src)[i]; + } + size = 0; + } else { + size -= PAGE_SIZE; + } + + *entry = INVALID_DESC; + dsbishst(); + tlbivae3is(TLBI_ADDR(base_va)); + dsbish(); + isb(); + *entry = desc; + + base_va += PAGE_SIZE; + src += PAGE_SIZE; + entry++; + } while (size > 0); + + dsbish(); + + if (reset) { + wfi(); + } + + return 0; +} diff --git a/services/std_svc/lfa/lfa.mk b/services/std_svc/lfa/lfa.mk index 53af69fd7..d3d5cd864 100644 --- a/services/std_svc/lfa/lfa.mk +++ b/services/std_svc/lfa/lfa.mk @@ -6,9 +6,15 @@ LFA_SOURCES += $(addprefix services/std_svc/lfa/, \ lfa_main.c \ - bl31_lfa.c \ lfa_holding_pen.c) ifeq (${ENABLE_RMM}, 1) LFA_SOURCES += services/std_svc/rmmd/rmmd_rmm_lfa.c endif + +ifeq (${ENABLE_LFA_BL31}, 1) +LFA_SOURCES += $(addprefix services/std_svc/lfa/, \ + bl31_lfa.c \ + lfa_relocatable.S \ + lfa_reset.c) +endif diff --git a/services/std_svc/lfa/lfa_holding_pen.c b/services/std_svc/lfa/lfa_holding_pen.c index 9aa1e8c30..39eac41a8 100644 --- a/services/std_svc/lfa/lfa_holding_pen.c +++ b/services/std_svc/lfa/lfa_holding_pen.c @@ -7,10 +7,12 @@ #include <string.h> +#include <common/bl_common.h> #include <common/debug.h> #include <lib/psci/psci_lib.h> #include <lib/spinlock.h> #include <lib/utils_def.h> +#include <lib/xlat_tables/xlat_tables_v2.h> #include <plat/common/platform.h> #include <services/lfa_holding_pen.h> @@ -18,7 +20,7 @@ static spinlock_t holding_lock; static spinlock_t activation_lock; -static uint32_t activation_count; +static volatile uint32_t activation_count; static enum lfa_retc activation_status; /** @@ -44,28 +46,31 @@ static enum lfa_retc activation_status; */ bool lfa_holding_start(void) { - bool status; - unsigned int no_of_cpus; + unsigned int no_of_cpus = psci_num_cpus_running_on_safe(plat_my_core_pos()); + bool last_cpu; + /* + * This lock ensures only one CPU uses the activation_count + * variable at a time. + */ spin_lock(&activation_lock); + /* First CPU to arrive locks the holding pen. */ if (activation_count == 0U) { - /* First CPU locks holding lock */ spin_lock(&holding_lock); } activation_count += 1U; + last_cpu = (activation_count == no_of_cpus); - no_of_cpus = psci_num_cpus_running_on_safe(plat_my_core_pos()); - status = (activation_count == no_of_cpus); - if (!status) { - VERBOSE("Hold, %d CPU left\n", - no_of_cpus - activation_count); + if (!last_cpu) { + VERBOSE("Hold, %d CPU left\n", no_of_cpus - activation_count); } + /* Release the lock once count is updated. */ spin_unlock(&activation_lock); - return status; + return last_cpu; } /** @@ -103,3 +108,42 @@ void lfa_holding_release(enum lfa_retc status) activation_status = status; spin_unlock(&holding_lock); } + +#if ENABLE_LFA_BL31 +void lfa_load_relocatable(void) +{ + int ret; + + /* + * The relocatable code area is outside of the normal BL31 image, so + * before we can use it we have to make it writeable then copy the + * relocatable code into it. Then once the copy is complete we must + * mark it read only and executable again. + */ + ret = xlat_change_mem_attributes(LFA_RELOCATABLE_CODE_START, + (LFA_RELOCATABLE_DATA_START - LFA_RELOCATABLE_CODE_START), + MT_MEMORY | MT_RW | MT_EXECUTE_NEVER | EL3_PAS); + if (ret != 0) { + ERROR("LFA: Could not update memory attributes: %d\n", ret); + panic(); + } + + memcpy((void *)LFA_RELOCATABLE_CODE_START, (void *)LFA_RELOCATABLE_LMA, + (uintptr_t)(LFA_RELOCATABLE_DATA_START - LFA_RELOCATABLE_CODE_START)); + + ret = xlat_change_mem_attributes(LFA_RELOCATABLE_CODE_START, + (LFA_RELOCATABLE_DATA_START - LFA_RELOCATABLE_CODE_START), + MT_MEMORY | MT_RO | MT_EXECUTE | EL3_PAS); + if (ret != 0) { + ERROR("LFA: Could not update memory attributes: %d\n", ret); + panic(); + } + + /* + * The relocatable data section contains unknown information at this + * point so zero it out before we use it. + */ + memset((void *)LFA_RELOCATABLE_DATA_START, 0, + (LFA_RELOCATABLE_DATA_END - LFA_RELOCATABLE_DATA_START)); +} +#endif /* ENABLE_LFA_BL31 */ diff --git a/services/std_svc/lfa/lfa_main.c b/services/std_svc/lfa/lfa_main.c index 819b169a7..31c05bf58 100644 --- a/services/std_svc/lfa/lfa_main.c +++ b/services/std_svc/lfa/lfa_main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Arm Limited. All rights reserved. + * Copyright (c) 2025-2026, Arm Limited. All rights reserved. * Copyright (c) 2025, NVIDIA Corporation. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause @@ -15,10 +15,23 @@ #include <services/rmmd_rmm_lfa.h> #include <smccc_helpers.h> -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; +static volatile bool activation_cancel_locked; /* * Spinlock to serialize LFA operations (PRIME, ACTIVATE). @@ -32,7 +45,12 @@ void lfa_reset_activation(void) { current_activation.component_id = LFA_INVALID_COMPONENT; current_activation.prime_status = PRIME_NONE; - current_activation.cpu_rendezvous_required = false; + current_activation.cpu_rendezvous = false; + activation_in_progress = false; + activation_skip_cpu_rendezvous = false; + activation_users = 0U; + activation_failed = false; + activation_cancel_locked = false; } static int convert_to_lfa_error(int ret) @@ -54,8 +72,7 @@ static bool lfa_initialize_components(void) lfa_component_count = plat_lfa_get_components(&lfa_components); if (lfa_component_count == 0U || lfa_components == NULL) { - /* unlikely to reach here */ - ERROR("Invalid LFA component setup: count = 0 or components are NULL"); + ERROR("Could not retrieve LFA components.\n"); return false; } @@ -64,8 +81,7 @@ static bool lfa_initialize_components(void) static uint64_t get_fw_activation_flags(uint32_t fw_seq_id) { - const plat_lfa_component_info_t *comp = - &lfa_components[fw_seq_id]; + const plat_lfa_component_info_t *comp = &lfa_components[fw_seq_id]; uint64_t flags = 0ULL; flags |= ((comp->activator == NULL ? 0ULL : 1ULL) @@ -86,6 +102,7 @@ static uint64_t get_fw_activation_flags(uint32_t fw_seq_id) static int lfa_cancel(uint32_t component_id) { int ret = LFA_SUCCESS; + struct lfa_component_ops *activator; if (lfa_component_count == 0U) { return LFA_WRONG_STATE; @@ -97,76 +114,33 @@ static int lfa_cancel(uint32_t component_id) return LFA_INVALID_PARAMETERS; } - ret = plat_lfa_cancel(component_id); - if (ret != LFA_SUCCESS) { + /* + * Deny cancellation once the activation round is no longer cancellable. + */ + if (activation_in_progress && + (activation_cancel_locked || activation_skip_cpu_rendezvous)) { return LFA_BUSY; } - /* TODO: add proper termination prime and activate phases */ - lfa_reset_activation(); - - 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; - } - - /* 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_components[component_id].activator == NULL) { - return LFA_NOT_SUPPORTED; - } - - ret = plat_lfa_notify_activate(component_id); - if (ret != 0) { - return LFA_ACTIVATION_FAILED; + activator = lfa_components[component_id].activator; + if (activator->cancel != NULL) { + ret = activator->cancel(¤t_activation); + if (ret != LFA_SUCCESS) { + return LFA_BUSY; + } } - activator = lfa_components[component_id].activator; - if (activator->activate != NULL) { + ret = plat_lfa_cancel(component_id); + if (ret != LFA_SUCCESS) { /* - * Pass skip_cpu_rendezvous (flag[0]) only if flag[0]==1 - * & CPU_RENDEZVOUS is not required. + * Cancellation can fail when activation cannot be stopped + * (e.g. activation requested with skip_cpu_rendezvous=1). */ - if (flags & LFA_SKIP_CPU_RENDEZVOUS_BIT) { - if (!activator->cpu_rendezvous_required) { - INFO("Skipping rendezvous requested by caller.\n"); - current_activation.cpu_rendezvous_required = false; - } - /* - * 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; - } - } - - ret = activator->activate(¤t_activation, ep_address, - context_id); + return LFA_BUSY; } - /* - * Update the activation pending flag only if the activation was - * successful. - */ - if (ret == LFA_SUCCESS) { - lfa_components[component_id].activation_pending = false; - } + /* TODO: add proper termination prime and activate phases */ + lfa_reset_activation(); return ret; } @@ -225,7 +199,13 @@ static int lfa_prime(uint32_t component_id, uint64_t *flags) } } + /* + * Update current activation status fields. CPU rendezvous is enabled + * by default but can be disabled if requested and allowed. + */ + current_activation.cpu_rendezvous = true; current_activation.prime_status = PRIME_COMPLETE; + current_activation.reset = lfa_components[component_id].activator->may_reset_cpu; } /* @@ -264,12 +244,122 @@ 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; + activation_cancel_locked = 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; + if (activation_users >= psci_num_cpus_running_on_safe(plat_my_core_pos())) { + activation_cancel_locked = true; + } + + 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; + activation_cancel_locked = 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) { - uint64_t retx1, retx2; - uint64_t lfa_flags; + uint64_t retx1; + uint64_t retx2; + uint64_t lfa_flags = 0; uint8_t *uuid_p; uint32_t fw_seq_id = (uint32_t)x1; int ret; @@ -292,9 +382,9 @@ uint64_t lfa_smc_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, break; case LFA_GET_INFO: - /** + /* * The current specification limits this input parameter to be zero for - * version 1.0 of LFA + * version 1.0 of LFA. */ if (x1 == 0ULL) { SMC_RET3(handle, LFA_SUCCESS, lfa_component_count, 0); @@ -309,29 +399,27 @@ uint64_t lfa_smc_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, } /* - * Check if fw_seq_id is in range. LFA_GET_INFO must be called first to scan - * platform firmware and create a valid number of firmware components. + * Check if fw_seq_id is in range. LFA_GET_INFO must be called + * first to scan platform firmware and create a valid number of + * firmware components. */ if (fw_seq_id >= lfa_component_count) { SMC_RET1(handle, LFA_INVALID_PARAMETERS); } - /* - * grab the UUID of asked fw_seq_id and set the return UUID - * variables - */ + /* Get the UUID of requested fw_seq_id. */ uuid_p = (uint8_t *)&lfa_components[fw_seq_id].uuid; memcpy(&retx1, uuid_p, sizeof(uint64_t)); memcpy(&retx2, uuid_p + sizeof(uint64_t), sizeof(uint64_t)); /* - * check the given fw_seq_id's update available - * and accordingly set the active_pending flag + * Check the given fw_seq_id update available and accordingly + * set the active_pending flag. */ lfa_components[fw_seq_id].activation_pending = is_plat_lfa_activation_pending(fw_seq_id); - INFO("Component %lu %s live activation:\n", x1, + INFO("Component %lu %s live activation.\n", x1, lfa_components[fw_seq_id].activator ? "supports" : "does not support"); @@ -340,10 +428,7 @@ uint64_t lfa_smc_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, lfa_components[fw_seq_id].activation_pending ? "true" : "false"); } - INFO("x1 = 0x%016lx, x2 = 0x%016lx\n", retx1, retx2); - SMC_RET4(handle, LFA_SUCCESS, retx1, retx2, get_fw_activation_flags(fw_seq_id)); - break; case LFA_PRIME: @@ -369,25 +454,111 @@ 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: + if (!spin_trylock(&lfa_lock)) { + SMC_RET1(handle, LFA_BUSY); + } + ret = lfa_cancel(x1); + + spin_unlock(&lfa_lock); + SMC_RET1(handle, ret); break; default: WARN("Unimplemented LFA Service Call: 0x%x\n", smc_fid); SMC_RET1(handle, SMC_UNK); - break; /* unreachable */ - + break; } - SMC_RET1(handle, SMC_UNK); - return 0; } diff --git a/services/std_svc/lfa/lfa_relocatable.S b/services/std_svc/lfa/lfa_relocatable.S new file mode 100644 index 000000000..7436584bb --- /dev/null +++ b/services/std_svc/lfa/lfa_relocatable.S @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2026, Arm Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include <asm_macros.S> +#include <platform_def.h> + +/* Relocatable code section */ + + .globl lfa_r_holding_lock + .globl lfa_r_holding_wait + .globl lfa_r_holding_unlock + .globl lfa_r_holding_wait_warm_reset + +/* + * The 'rfunc' macro is a modified version of the 'func' macro in + * "include/common/asm_macros_common.S". It differs in that it does not change + * the section to ".text.asm". See the original version for comments. + */ + .macro rfunc _name, _align=2 + .cfi_sections .debug_frame + .section .lfa_relocatable_code.\_name, "ax" + .type \_name, %function + .cfi_startproc + .align \_align + \_name: +#if ENABLE_BTI + bti jc +#endif + .endm + +/* + * Enable holding lock. + * + * void lfa_r_holding_lock(spinlock_t *lock); + */ +rfunc lfa_r_holding_lock + mov w1, #1 + stlr w1, [x0] + ret +endfunc lfa_r_holding_lock + +/* + * Release hold. + * + * Use store-release to unconditionally clear the spinlock variable. + * Store operation generates an event to all cores waiting in WFE + * when address is monitored by the global monitor. + * + * void lfa_r_holding_unlock(spinlock_t *lock); + */ +rfunc lfa_r_holding_unlock + mov w1, #0 + stlr w1, [x0] + ret +endfunc lfa_r_holding_unlock + +/* + * Check lock using load-exclusive instruction. + * + * void lfa_r_holding_wait(spinlock_t *lock); + */ +rfunc lfa_r_holding_wait + sevl +l1: wfe + ldaxr w1, [x0] + cbnz w1, l1 + ret +endfunc lfa_r_holding_wait + +/* + * Check lock using load-exclusive instruction. + * Issue RMR_EL3 for warm reset if lock is unlocked + * + * void lfa_r_holding_wait(spinlock_t *lock); + */ +rfunc lfa_r_holding_wait_warm_reset + sevl +l2: + wfe + ldaxr w1, [x0] + cbnz w1, l2 +l3: + wfi + b l3 +endfunc lfa_r_holding_wait_warm_reset + +/* Relocatable data section */ + + .globl lfa_r_holding_lock_var + .globl lfa_r_ep_addresses + .globl lfa_r_context_ids + +.align 8 +.section .lfa_relocatable_data.lfa_r_holding_lock_var, "aw" +lfa_r_holding_lock_var: .quad 0x0 + +.align 8 +.section .lfa_relocatable_data.lfa_r_ep_addresses, "aw" +lfa_r_ep_addresses: + .fill PLATFORM_CORE_COUNT, 8, 0x0 + +.align 8 +.section .lfa_relocatable_data.lfa_r_context_ids, "aw" +lfa_r_context_ids: + .fill PLATFORM_CORE_COUNT, 8, 0x0 diff --git a/services/std_svc/lfa/lfa_reset.c b/services/std_svc/lfa/lfa_reset.c new file mode 100644 index 000000000..07db8cc97 --- /dev/null +++ b/services/std_svc/lfa/lfa_reset.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2026, Arm Limited. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include <common/build_message.h> +#include <lib/el3_runtime/context_mgmt.h> +#include <lib/psci/psci.h> +#include <lib/spinlock.h> +#include <plat/arm/common/plat_arm.h> +#include <plat/common/platform.h> +#include <services/lfa_svc.h> + +/* + * These variables are used pre and post live activation so place them in the + * relocatable data section to ensure they don't get moved around and persist + * across live activation. + */ +static lfa_mailbox_message_t lfa_mailbox[PLATFORM_CORE_COUNT] __attribute__((section(".lfa_relocatable_data"))); +static const plat_psci_ops_t *lfa_psci_ops __attribute__((section(".lfa_relocatable_data"))); +static uintptr_t plat_wr_ep __attribute__((section(".lfa_relocatable_data"))); +static uint64_t reset_cpu_count __attribute__((section(".lfa_relocatable_data"))); +static spinlock_t cpucountlock __attribute__((section(".lfa_relocatable_data"))); + +extern plat_psci_ops_t *psci_plat_pm_ops; +extern int psci_validate_entry_point(entry_point_info_t *ep, + uintptr_t entrypoint, + u_register_t context_id); +extern void bl31_lfa_entrypoint(void); +extern void bl31_warm_entrypoint(void); + +static void add_reset_cpu(void) +{ + spin_lock(&cpucountlock); + reset_cpu_count++; + spin_unlock(&cpucountlock); +} + +static uint64_t remove_reset_cpu(void) +{ + uint64_t cpu_count; + + spin_lock(&cpucountlock); + if (reset_cpu_count > 0) { + reset_cpu_count--; + } + cpu_count = reset_cpu_count; + spin_unlock(&cpucountlock); + + return cpu_count; +} + +/* + * Prepare CPU warm reset. This functioned should be called before entering + * relocatable code for each CPU. + */ +uint64_t prepare_warm_reset(uintptr_t lfa_ns_ep, uint64_t context_id, uint64_t cpu_spinlock) +{ + const psci_power_state_t state_info = { {ARM_LOCAL_STATE_OFF} }; + unsigned int core_pos = plat_my_core_pos(); + lfa_mailbox_message_t mmesg; + int rc; + static spinlock_t setuplock; + + spin_lock(&setuplock); + if (plat_wr_ep == 0 && plat_lfa_mailbox_base()) { + plat_wr_ep = *((uintptr_t *)plat_lfa_mailbox_base()); + } + + if (lfa_psci_ops == NULL) { + plat_setup_psci_ops((uintptr_t)bl31_lfa_entrypoint, &lfa_psci_ops); + } + spin_unlock(&setuplock); + + rc = psci_validate_entry_point(&mmesg.ep, lfa_ns_ep, (u_register_t)context_id); + if (rc != PSCI_E_SUCCESS) { + ERROR("BL31 LFA: Invalid NS entrypoint! (error %d)\n", rc); + return LFA_CRITICAL_ERROR; + } + + /* Stash the holding pen lock so we can release it after reboot. */ + mmesg.spinlock_p = (uint64_t)cpu_spinlock; + + /* Stash the platform warmboot entry point so we can restore it. */ + mmesg.plat_wr_mailbox = plat_wr_ep; + memcpy((void *)&lfa_mailbox[core_pos], &mmesg, sizeof(lfa_mailbox_message_t)); + + /* + * TODO: refactor PSCI suspend API to be usable here. The main issue is that + * PSCI suspend will put the core down immediately, but the use case here is + * slightly different since we have to do the firmware copy operation at the + * very end right before putting the core down for reset. + */ + if (lfa_psci_ops->pwr_domain_suspend != NULL) { + /* + * We can't use the proper PSCI API since it will put the core + * down immediately, we still need to copy BL31. + */ +#if USE_GIC_DRIVER + gic_cpuif_disable(core_pos); +#endif + lfa_psci_ops->pwr_domain_suspend(&state_info); + } else { + ERROR("BL31: PSCI power domain suspend not supported!\n"); + return LFA_CRITICAL_ERROR; + } + + add_reset_cpu(); + + cm_el2_sysregs_context_save(NON_SECURE); + + return LFA_SUCCESS; +} + +/* + * Entrypoint function for CPU warm reset case. + */ +void __no_pauth lfa_warm_reset_entrypoint(void) +{ + static spinlock_t resetlock; + uint32_t counter_freq; + const psci_power_state_t state_info = { {ARM_LOCAL_STATE_OFF} }; + lfa_mailbox_message_t mmesg; + unsigned int core_pos = plat_my_core_pos(); + +#if ENABLE_FEAT_RME + /* + * At warm boot GPT data structures have already been initialized in RAM + * but the sysregs for this CPU need to be initialized. Note that the GPT + * accesses are controlled attributes in GPCCR and do not depend on the + * SCR_EL3.C bit. + */ + if (gpt_enable() != 0) { + panic(); + } +#endif + + /* TODO see if we can use psci_warmboot_entrypoint here. */ + psci_plat_pm_ops->pwr_domain_suspend_finish(&state_info); +#if USE_GIC_DRIVER + gic_cpuif_enable(core_pos); +#endif + + /* Init registers that never change for the lifetime of TF-A */ + cm_manage_extensions_el3(core_pos); + + /* Re-init the cntfrq_el0 register */ + counter_freq = plat_get_syscnt_freq2(); + write_cntfrq_el0(counter_freq); + + memcpy(&mmesg, (void *)&lfa_mailbox[core_pos], sizeof(lfa_mailbox_message_t)); + + spin_lock(&resetlock); + NOTICE("BL31 LFA Warm Reset on CPU %u\n", core_pos); + NOTICE(" Version : %s\n", build_version_string); + NOTICE(" %s\n", build_message); + console_flush(); + spin_unlock(&resetlock); + + /* The last CPU to reach here restores the warm entrypoint. */ + if (remove_reset_cpu() == 0) { + plat_arm_program_trusted_mailbox(mmesg.plat_wr_mailbox); + plat_wr_ep = 0; + lfa_psci_ops = NULL; + } + + spin_unlock((spinlock_t *)mmesg.spinlock_p); + + cm_init_my_context(&mmesg.ep); + + cm_prepare_el3_exit_ns(); +} |
