1b058f20aSOlivier Deprez /* 2cdb49d47SOlivier Deprez * Copyright (c) 2020-2021, ARM Limited and Contributors. All rights reserved. 3b058f20aSOlivier Deprez * 4b058f20aSOlivier Deprez * SPDX-License-Identifier: BSD-3-Clause 5b058f20aSOlivier Deprez */ 6b058f20aSOlivier Deprez 7b058f20aSOlivier Deprez #include <assert.h> 8f0d743dbSOlivier Deprez #include <errno.h> 9a92bc73bSOlivier Deprez #include <lib/el3_runtime/context_mgmt.h> 10473ced56SOlivier Deprez #include <lib/spinlock.h> 11b058f20aSOlivier Deprez #include "spmd_private.h" 12b058f20aSOlivier Deprez 13cdb49d47SOlivier Deprez static struct { 14cdb49d47SOlivier Deprez bool secondary_ep_locked; 15cdb49d47SOlivier Deprez uintptr_t secondary_ep; 16473ced56SOlivier Deprez spinlock_t lock; 17cdb49d47SOlivier Deprez } g_spmd_pm; 18cdb49d47SOlivier Deprez 19f0d743dbSOlivier Deprez /******************************************************************************* 20a92bc73bSOlivier Deprez * spmd_build_spmc_message 21a92bc73bSOlivier Deprez * 22a92bc73bSOlivier Deprez * Builds an SPMD to SPMC direct message request. 23a92bc73bSOlivier Deprez ******************************************************************************/ 24a92bc73bSOlivier Deprez static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message) 25a92bc73bSOlivier Deprez { 26a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32); 27a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X1, 28a92bc73bSOlivier Deprez (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) | 29a92bc73bSOlivier Deprez spmd_spmc_id_get()); 30a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ); 31a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X3, message); 32a92bc73bSOlivier Deprez } 33a92bc73bSOlivier Deprez 34a92bc73bSOlivier Deprez /******************************************************************************* 35cdb49d47SOlivier Deprez * spmd_pm_secondary_ep_register 36f0d743dbSOlivier Deprez ******************************************************************************/ 37cdb49d47SOlivier Deprez int spmd_pm_secondary_ep_register(uintptr_t entry_point) 38f0d743dbSOlivier Deprez { 39473ced56SOlivier Deprez int ret = FFA_ERROR_INVALID_PARAMETER; 40473ced56SOlivier Deprez 41473ced56SOlivier Deprez spin_lock(&g_spmd_pm.lock); 42473ced56SOlivier Deprez 43cdb49d47SOlivier Deprez if (g_spmd_pm.secondary_ep_locked == true) { 44473ced56SOlivier Deprez goto out; 45f0d743dbSOlivier Deprez } 46f0d743dbSOlivier Deprez 47f0d743dbSOlivier Deprez /* 48f0d743dbSOlivier Deprez * Check entry_point address is a PA within 49f0d743dbSOlivier Deprez * load_address <= entry_point < load_address + binary_size 50f0d743dbSOlivier Deprez */ 51f0d743dbSOlivier Deprez if (!spmd_check_address_in_binary_image(entry_point)) { 52cdb49d47SOlivier Deprez ERROR("%s entry point is not within image boundaries\n", 53cdb49d47SOlivier Deprez __func__); 54473ced56SOlivier Deprez goto out; 55f0d743dbSOlivier Deprez } 56f0d743dbSOlivier Deprez 57cdb49d47SOlivier Deprez g_spmd_pm.secondary_ep = entry_point; 58cdb49d47SOlivier Deprez g_spmd_pm.secondary_ep_locked = true; 5902d50bb0SOlivier Deprez 60cdb49d47SOlivier Deprez VERBOSE("%s %lx\n", __func__, entry_point); 61f0d743dbSOlivier Deprez 62473ced56SOlivier Deprez ret = 0; 63473ced56SOlivier Deprez 64473ced56SOlivier Deprez out: 65473ced56SOlivier Deprez spin_unlock(&g_spmd_pm.lock); 66473ced56SOlivier Deprez 67473ced56SOlivier Deprez return ret; 68f0d743dbSOlivier Deprez } 69f0d743dbSOlivier Deprez 70b058f20aSOlivier Deprez /******************************************************************************* 71b058f20aSOlivier Deprez * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part 72b058f20aSOlivier Deprez * of the SPMC initialization path, they will initialize any SPs that they 73b058f20aSOlivier Deprez * manage. Entry into SPMC is done after initialising minimal architectural 74b058f20aSOlivier Deprez * state that guarantees safe execution. 75b058f20aSOlivier Deprez ******************************************************************************/ 76b058f20aSOlivier Deprez static void spmd_cpu_on_finish_handler(u_register_t unused) 77b058f20aSOlivier Deprez { 78b058f20aSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 79a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 80*f2dcf418SOlivier Deprez el3_state_t *el3_state; 81*f2dcf418SOlivier Deprez uintptr_t entry_point; 8202d50bb0SOlivier Deprez uint64_t rc; 83b058f20aSOlivier Deprez 84a92bc73bSOlivier Deprez assert(ctx != NULL); 85b058f20aSOlivier Deprez assert(ctx->state != SPMC_STATE_ON); 86a92bc73bSOlivier Deprez 87473ced56SOlivier Deprez spin_lock(&g_spmd_pm.lock); 88473ced56SOlivier Deprez 89a92bc73bSOlivier Deprez /* 90cdb49d47SOlivier Deprez * Leave the possibility that the SPMC does not call 91cdb49d47SOlivier Deprez * FFA_SECONDARY_EP_REGISTER in which case re-use the 92cdb49d47SOlivier Deprez * primary core address for booting secondary cores. 93a92bc73bSOlivier Deprez */ 94cdb49d47SOlivier Deprez if (g_spmd_pm.secondary_ep_locked == true) { 95*f2dcf418SOlivier Deprez /* 96*f2dcf418SOlivier Deprez * The CPU context has already been initialized at boot time 97*f2dcf418SOlivier Deprez * (in spmd_spmc_init by a call to cm_setup_context). Adjust 98*f2dcf418SOlivier Deprez * below the target core entry point based on the address 99*f2dcf418SOlivier Deprez * passed to by FFA_SECONDARY_EP_REGISTER. 100*f2dcf418SOlivier Deprez */ 101*f2dcf418SOlivier Deprez entry_point = g_spmd_pm.secondary_ep; 102*f2dcf418SOlivier Deprez el3_state = get_el3state_ctx(&ctx->cpu_ctx); 103*f2dcf418SOlivier Deprez write_ctx_reg(el3_state, CTX_ELR_EL3, entry_point); 104a92bc73bSOlivier Deprez } 105a92bc73bSOlivier Deprez 106473ced56SOlivier Deprez spin_unlock(&g_spmd_pm.lock); 107473ced56SOlivier Deprez 108*f2dcf418SOlivier Deprez /* Mark CPU as initiating ON operation. */ 109a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING; 110b058f20aSOlivier Deprez 111b058f20aSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx); 11202d50bb0SOlivier Deprez if (rc != 0ULL) { 11302d50bb0SOlivier Deprez ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, 114b058f20aSOlivier Deprez linear_id); 115b058f20aSOlivier Deprez ctx->state = SPMC_STATE_OFF; 116b058f20aSOlivier Deprez return; 117b058f20aSOlivier Deprez } 118b058f20aSOlivier Deprez 119b058f20aSOlivier Deprez ctx->state = SPMC_STATE_ON; 120a92bc73bSOlivier Deprez 121a92bc73bSOlivier Deprez VERBOSE("CPU %u on!\n", linear_id); 122a92bc73bSOlivier Deprez } 123a92bc73bSOlivier Deprez 124a92bc73bSOlivier Deprez /******************************************************************************* 125a92bc73bSOlivier Deprez * spmd_cpu_off_handler 126a92bc73bSOlivier Deprez ******************************************************************************/ 127a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused) 128a92bc73bSOlivier Deprez { 129a92bc73bSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 130a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 13102d50bb0SOlivier Deprez int64_t rc; 132a92bc73bSOlivier Deprez 133a92bc73bSOlivier Deprez assert(ctx != NULL); 134a92bc73bSOlivier Deprez assert(ctx->state != SPMC_STATE_OFF); 135a92bc73bSOlivier Deprez 136a92bc73bSOlivier Deprez /* Build an SPMD to SPMC direct message request. */ 137a92bc73bSOlivier Deprez spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF); 138a92bc73bSOlivier Deprez 139a92bc73bSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx); 14002d50bb0SOlivier Deprez if (rc != 0ULL) { 14102d50bb0SOlivier Deprez ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, linear_id); 142a92bc73bSOlivier Deprez } 143a92bc73bSOlivier Deprez 144cdb49d47SOlivier Deprez /* Expect a direct message response from the SPMC. */ 145cdb49d47SOlivier Deprez u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx), 146cdb49d47SOlivier Deprez CTX_GPREG_X0); 147cdb49d47SOlivier Deprez if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) { 148cdb49d47SOlivier Deprez ERROR("%s invalid SPMC response (%lx).\n", 149cdb49d47SOlivier Deprez __func__, ffa_resp_func); 150cdb49d47SOlivier Deprez return -EINVAL; 151cdb49d47SOlivier Deprez } 152a92bc73bSOlivier Deprez 153a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_OFF; 154a92bc73bSOlivier Deprez 155a92bc73bSOlivier Deprez VERBOSE("CPU %u off!\n", linear_id); 156a92bc73bSOlivier Deprez 157a92bc73bSOlivier Deprez return 0; 158b058f20aSOlivier Deprez } 159b058f20aSOlivier Deprez 160b058f20aSOlivier Deprez /******************************************************************************* 161b058f20aSOlivier Deprez * Structure populated by the SPM Dispatcher to perform any bookkeeping before 162b058f20aSOlivier Deprez * PSCI executes a power mgmt. operation. 163b058f20aSOlivier Deprez ******************************************************************************/ 164b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = { 165b058f20aSOlivier Deprez .svc_on_finish = spmd_cpu_on_finish_handler, 166a92bc73bSOlivier Deprez .svc_off = spmd_cpu_off_handler 167b058f20aSOlivier Deprez }; 168