1b058f20aSOlivier Deprez /* 2*cdb49d47SOlivier 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> 10b058f20aSOlivier Deprez #include "spmd_private.h" 11b058f20aSOlivier Deprez 12*cdb49d47SOlivier Deprez static struct { 13*cdb49d47SOlivier Deprez bool secondary_ep_locked; 14*cdb49d47SOlivier Deprez uintptr_t secondary_ep; 15*cdb49d47SOlivier Deprez } g_spmd_pm; 16*cdb49d47SOlivier Deprez 17f0d743dbSOlivier Deprez /******************************************************************************* 18a92bc73bSOlivier Deprez * spmd_build_spmc_message 19a92bc73bSOlivier Deprez * 20a92bc73bSOlivier Deprez * Builds an SPMD to SPMC direct message request. 21a92bc73bSOlivier Deprez ******************************************************************************/ 22a92bc73bSOlivier Deprez static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message) 23a92bc73bSOlivier Deprez { 24a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32); 25a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X1, 26a92bc73bSOlivier Deprez (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) | 27a92bc73bSOlivier Deprez spmd_spmc_id_get()); 28a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ); 29a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X3, message); 30a92bc73bSOlivier Deprez } 31a92bc73bSOlivier Deprez 32a92bc73bSOlivier Deprez /******************************************************************************* 33*cdb49d47SOlivier Deprez * spmd_pm_secondary_ep_register 34f0d743dbSOlivier Deprez ******************************************************************************/ 35*cdb49d47SOlivier Deprez int spmd_pm_secondary_ep_register(uintptr_t entry_point) 36f0d743dbSOlivier Deprez { 37*cdb49d47SOlivier Deprez if (g_spmd_pm.secondary_ep_locked == true) { 38*cdb49d47SOlivier Deprez return FFA_ERROR_INVALID_PARAMETER; 39f0d743dbSOlivier Deprez } 40f0d743dbSOlivier Deprez 41f0d743dbSOlivier Deprez /* 42f0d743dbSOlivier Deprez * Check entry_point address is a PA within 43f0d743dbSOlivier Deprez * load_address <= entry_point < load_address + binary_size 44f0d743dbSOlivier Deprez */ 45f0d743dbSOlivier Deprez if (!spmd_check_address_in_binary_image(entry_point)) { 46*cdb49d47SOlivier Deprez ERROR("%s entry point is not within image boundaries\n", 47*cdb49d47SOlivier Deprez __func__); 48*cdb49d47SOlivier Deprez return FFA_ERROR_INVALID_PARAMETER; 49f0d743dbSOlivier Deprez } 50f0d743dbSOlivier Deprez 51*cdb49d47SOlivier Deprez g_spmd_pm.secondary_ep = entry_point; 52*cdb49d47SOlivier Deprez g_spmd_pm.secondary_ep_locked = true; 5302d50bb0SOlivier Deprez 54*cdb49d47SOlivier Deprez VERBOSE("%s %lx\n", __func__, entry_point); 55f0d743dbSOlivier Deprez 56f0d743dbSOlivier Deprez return 0; 57f0d743dbSOlivier Deprez } 58f0d743dbSOlivier Deprez 59b058f20aSOlivier Deprez /******************************************************************************* 60b058f20aSOlivier Deprez * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part 61b058f20aSOlivier Deprez * of the SPMC initialization path, they will initialize any SPs that they 62b058f20aSOlivier Deprez * manage. Entry into SPMC is done after initialising minimal architectural 63b058f20aSOlivier Deprez * state that guarantees safe execution. 64b058f20aSOlivier Deprez ******************************************************************************/ 65b058f20aSOlivier Deprez static void spmd_cpu_on_finish_handler(u_register_t unused) 66b058f20aSOlivier Deprez { 67a92bc73bSOlivier Deprez entry_point_info_t *spmc_ep_info = spmd_spmc_ep_info_get(); 68b058f20aSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 69a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 7002d50bb0SOlivier Deprez uint64_t rc; 71b058f20aSOlivier Deprez 72a92bc73bSOlivier Deprez assert(ctx != NULL); 73b058f20aSOlivier Deprez assert(ctx->state != SPMC_STATE_ON); 74a92bc73bSOlivier Deprez assert(spmc_ep_info != NULL); 75a92bc73bSOlivier Deprez 76a92bc73bSOlivier Deprez /* 77*cdb49d47SOlivier Deprez * Leave the possibility that the SPMC does not call 78*cdb49d47SOlivier Deprez * FFA_SECONDARY_EP_REGISTER in which case re-use the 79*cdb49d47SOlivier Deprez * primary core address for booting secondary cores. 80a92bc73bSOlivier Deprez */ 81*cdb49d47SOlivier Deprez if (g_spmd_pm.secondary_ep_locked == true) { 82*cdb49d47SOlivier Deprez spmc_ep_info->pc = g_spmd_pm.secondary_ep; 83a92bc73bSOlivier Deprez } 84a92bc73bSOlivier Deprez 85a92bc73bSOlivier Deprez cm_setup_context(&ctx->cpu_ctx, spmc_ep_info); 86a92bc73bSOlivier Deprez 87a92bc73bSOlivier Deprez /* Mark CPU as initiating ON operation */ 88a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING; 89b058f20aSOlivier Deprez 90b058f20aSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx); 9102d50bb0SOlivier Deprez if (rc != 0ULL) { 9202d50bb0SOlivier Deprez ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, 93b058f20aSOlivier Deprez linear_id); 94b058f20aSOlivier Deprez ctx->state = SPMC_STATE_OFF; 95b058f20aSOlivier Deprez return; 96b058f20aSOlivier Deprez } 97b058f20aSOlivier Deprez 98b058f20aSOlivier Deprez ctx->state = SPMC_STATE_ON; 99a92bc73bSOlivier Deprez 100a92bc73bSOlivier Deprez VERBOSE("CPU %u on!\n", linear_id); 101a92bc73bSOlivier Deprez } 102a92bc73bSOlivier Deprez 103a92bc73bSOlivier Deprez /******************************************************************************* 104a92bc73bSOlivier Deprez * spmd_cpu_off_handler 105a92bc73bSOlivier Deprez ******************************************************************************/ 106a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused) 107a92bc73bSOlivier Deprez { 108a92bc73bSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 109a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 11002d50bb0SOlivier Deprez int64_t rc; 111a92bc73bSOlivier Deprez 112a92bc73bSOlivier Deprez assert(ctx != NULL); 113a92bc73bSOlivier Deprez assert(ctx->state != SPMC_STATE_OFF); 114a92bc73bSOlivier Deprez 115a92bc73bSOlivier Deprez /* Build an SPMD to SPMC direct message request. */ 116a92bc73bSOlivier Deprez spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF); 117a92bc73bSOlivier Deprez 118a92bc73bSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx); 11902d50bb0SOlivier Deprez if (rc != 0ULL) { 12002d50bb0SOlivier Deprez ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, linear_id); 121a92bc73bSOlivier Deprez } 122a92bc73bSOlivier Deprez 123*cdb49d47SOlivier Deprez /* Expect a direct message response from the SPMC. */ 124*cdb49d47SOlivier Deprez u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx), 125*cdb49d47SOlivier Deprez CTX_GPREG_X0); 126*cdb49d47SOlivier Deprez if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) { 127*cdb49d47SOlivier Deprez ERROR("%s invalid SPMC response (%lx).\n", 128*cdb49d47SOlivier Deprez __func__, ffa_resp_func); 129*cdb49d47SOlivier Deprez return -EINVAL; 130*cdb49d47SOlivier Deprez } 131a92bc73bSOlivier Deprez 132a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_OFF; 133a92bc73bSOlivier Deprez 134a92bc73bSOlivier Deprez VERBOSE("CPU %u off!\n", linear_id); 135a92bc73bSOlivier Deprez 136a92bc73bSOlivier Deprez return 0; 137b058f20aSOlivier Deprez } 138b058f20aSOlivier Deprez 139b058f20aSOlivier Deprez /******************************************************************************* 140b058f20aSOlivier Deprez * Structure populated by the SPM Dispatcher to perform any bookkeeping before 141b058f20aSOlivier Deprez * PSCI executes a power mgmt. operation. 142b058f20aSOlivier Deprez ******************************************************************************/ 143b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = { 144b058f20aSOlivier Deprez .svc_on_finish = spmd_cpu_on_finish_handler, 145a92bc73bSOlivier Deprez .svc_off = spmd_cpu_off_handler 146b058f20aSOlivier Deprez }; 147