1b058f20aSOlivier Deprez /* 2b058f20aSOlivier Deprez * Copyright (c) 2020, 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 12f0d743dbSOlivier Deprez /******************************************************************************* 13a92bc73bSOlivier Deprez * spmd_build_spmc_message 14a92bc73bSOlivier Deprez * 15a92bc73bSOlivier Deprez * Builds an SPMD to SPMC direct message request. 16a92bc73bSOlivier Deprez ******************************************************************************/ 17a92bc73bSOlivier Deprez static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message) 18a92bc73bSOlivier Deprez { 19a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32); 20a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X1, 21a92bc73bSOlivier Deprez (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) | 22a92bc73bSOlivier Deprez spmd_spmc_id_get()); 23a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ); 24a92bc73bSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X3, message); 25a92bc73bSOlivier Deprez } 26a92bc73bSOlivier Deprez 27a92bc73bSOlivier Deprez /******************************************************************************* 28f0d743dbSOlivier Deprez * spmd_pm_secondary_core_set_ep 29f0d743dbSOlivier Deprez ******************************************************************************/ 30a92bc73bSOlivier Deprez int spmd_pm_secondary_core_set_ep(unsigned long long mpidr, 31a92bc73bSOlivier Deprez uintptr_t entry_point, unsigned long long context) 32f0d743dbSOlivier Deprez { 33f0d743dbSOlivier Deprez int id = plat_core_pos_by_mpidr(mpidr); 34f0d743dbSOlivier Deprez 35f0d743dbSOlivier Deprez if ((id < 0) || (id >= PLATFORM_CORE_COUNT)) { 36f0d743dbSOlivier Deprez ERROR("%s inconsistent MPIDR (%llx)\n", __func__, mpidr); 37f0d743dbSOlivier Deprez return -EINVAL; 38f0d743dbSOlivier Deprez } 39f0d743dbSOlivier Deprez 40f0d743dbSOlivier Deprez /* 41f0d743dbSOlivier Deprez * Check entry_point address is a PA within 42f0d743dbSOlivier Deprez * load_address <= entry_point < load_address + binary_size 43f0d743dbSOlivier Deprez */ 44f0d743dbSOlivier Deprez if (!spmd_check_address_in_binary_image(entry_point)) { 45f0d743dbSOlivier Deprez ERROR("%s entry point is not within image boundaries (%llx)\n", 46f0d743dbSOlivier Deprez __func__, mpidr); 47f0d743dbSOlivier Deprez return -EINVAL; 48f0d743dbSOlivier Deprez } 49f0d743dbSOlivier Deprez 50*02d50bb0SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context_by_mpidr(mpidr); 51*02d50bb0SOlivier Deprez spmd_pm_secondary_ep_t *secondary_ep = &ctx->secondary_ep; 52*02d50bb0SOlivier Deprez if (secondary_ep->locked) { 53*02d50bb0SOlivier Deprez ERROR("%s entry locked (%llx)\n", __func__, mpidr); 54*02d50bb0SOlivier Deprez return -EINVAL; 55*02d50bb0SOlivier Deprez } 56*02d50bb0SOlivier Deprez 57f0d743dbSOlivier Deprez /* Fill new entry to corresponding secondary core id and lock it */ 58*02d50bb0SOlivier Deprez secondary_ep->entry_point = entry_point; 59*02d50bb0SOlivier Deprez secondary_ep->context = context; 60*02d50bb0SOlivier Deprez secondary_ep->locked = true; 61f0d743dbSOlivier Deprez 62f0d743dbSOlivier Deprez VERBOSE("%s %d %llx %lx %llx\n", 63f0d743dbSOlivier Deprez __func__, id, mpidr, entry_point, context); 64f0d743dbSOlivier Deprez 65f0d743dbSOlivier Deprez return 0; 66f0d743dbSOlivier Deprez } 67f0d743dbSOlivier Deprez 68b058f20aSOlivier Deprez /******************************************************************************* 69b058f20aSOlivier Deprez * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part 70b058f20aSOlivier Deprez * of the SPMC initialization path, they will initialize any SPs that they 71b058f20aSOlivier Deprez * manage. Entry into SPMC is done after initialising minimal architectural 72b058f20aSOlivier Deprez * state that guarantees safe execution. 73b058f20aSOlivier Deprez ******************************************************************************/ 74b058f20aSOlivier Deprez static void spmd_cpu_on_finish_handler(u_register_t unused) 75b058f20aSOlivier Deprez { 76a92bc73bSOlivier Deprez entry_point_info_t *spmc_ep_info = spmd_spmc_ep_info_get(); 77b058f20aSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 78a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 79*02d50bb0SOlivier Deprez uint64_t rc; 80b058f20aSOlivier Deprez 81a92bc73bSOlivier Deprez assert(ctx != NULL); 82b058f20aSOlivier Deprez assert(ctx->state != SPMC_STATE_ON); 83a92bc73bSOlivier Deprez assert(spmc_ep_info != NULL); 84a92bc73bSOlivier Deprez 85a92bc73bSOlivier Deprez /* 86a92bc73bSOlivier Deprez * TODO: this might require locking the spmc_ep_info structure, 87a92bc73bSOlivier Deprez * or provisioning one structure per cpu 88a92bc73bSOlivier Deprez */ 89*02d50bb0SOlivier Deprez if (ctx->secondary_ep.entry_point == 0UL) { 90a92bc73bSOlivier Deprez goto exit; 91a92bc73bSOlivier Deprez } 92a92bc73bSOlivier Deprez 93*02d50bb0SOlivier Deprez spmc_ep_info->pc = ctx->secondary_ep.entry_point; 94a92bc73bSOlivier Deprez cm_setup_context(&ctx->cpu_ctx, spmc_ep_info); 95a92bc73bSOlivier Deprez write_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx), CTX_GPREG_X0, 96*02d50bb0SOlivier Deprez ctx->secondary_ep.context); 97a92bc73bSOlivier Deprez 98a92bc73bSOlivier Deprez /* Mark CPU as initiating ON operation */ 99a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING; 100b058f20aSOlivier Deprez 101b058f20aSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx); 102*02d50bb0SOlivier Deprez if (rc != 0ULL) { 103*02d50bb0SOlivier Deprez ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, 104b058f20aSOlivier Deprez linear_id); 105b058f20aSOlivier Deprez ctx->state = SPMC_STATE_OFF; 106b058f20aSOlivier Deprez return; 107b058f20aSOlivier Deprez } 108b058f20aSOlivier Deprez 109a92bc73bSOlivier Deprez exit: 110b058f20aSOlivier Deprez ctx->state = SPMC_STATE_ON; 111a92bc73bSOlivier Deprez 112a92bc73bSOlivier Deprez VERBOSE("CPU %u on!\n", linear_id); 113a92bc73bSOlivier Deprez } 114a92bc73bSOlivier Deprez 115a92bc73bSOlivier Deprez /******************************************************************************* 116a92bc73bSOlivier Deprez * spmd_cpu_off_handler 117a92bc73bSOlivier Deprez ******************************************************************************/ 118a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused) 119a92bc73bSOlivier Deprez { 120a92bc73bSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 121a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 122*02d50bb0SOlivier Deprez int64_t rc; 123a92bc73bSOlivier Deprez 124a92bc73bSOlivier Deprez assert(ctx != NULL); 125a92bc73bSOlivier Deprez assert(ctx->state != SPMC_STATE_OFF); 126a92bc73bSOlivier Deprez 127*02d50bb0SOlivier Deprez if (ctx->secondary_ep.entry_point == 0UL) { 128a92bc73bSOlivier Deprez goto exit; 129a92bc73bSOlivier Deprez } 130a92bc73bSOlivier Deprez 131a92bc73bSOlivier Deprez /* Build an SPMD to SPMC direct message request. */ 132a92bc73bSOlivier Deprez spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF); 133a92bc73bSOlivier Deprez 134a92bc73bSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx); 135*02d50bb0SOlivier Deprez if (rc != 0ULL) { 136*02d50bb0SOlivier Deprez ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, linear_id); 137a92bc73bSOlivier Deprez } 138a92bc73bSOlivier Deprez 139a92bc73bSOlivier Deprez /* TODO expect FFA_DIRECT_MSG_RESP returned from SPMC */ 140a92bc73bSOlivier Deprez 141a92bc73bSOlivier Deprez exit: 142a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_OFF; 143a92bc73bSOlivier Deprez 144a92bc73bSOlivier Deprez VERBOSE("CPU %u off!\n", linear_id); 145a92bc73bSOlivier Deprez 146a92bc73bSOlivier Deprez return 0; 147b058f20aSOlivier Deprez } 148b058f20aSOlivier Deprez 149b058f20aSOlivier Deprez /******************************************************************************* 150b058f20aSOlivier Deprez * Structure populated by the SPM Dispatcher to perform any bookkeeping before 151b058f20aSOlivier Deprez * PSCI executes a power mgmt. operation. 152b058f20aSOlivier Deprez ******************************************************************************/ 153b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = { 154b058f20aSOlivier Deprez .svc_on_finish = spmd_cpu_on_finish_handler, 155a92bc73bSOlivier Deprez .svc_off = spmd_cpu_off_handler 156b058f20aSOlivier Deprez }; 157