1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #include <assert.h> 8*91f16700Schasinglulu #include <errno.h> 9*91f16700Schasinglulu 10*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h> 11*91f16700Schasinglulu #include <lib/spinlock.h> 12*91f16700Schasinglulu #include <plat/common/common_def.h> 13*91f16700Schasinglulu #include <plat/common/platform.h> 14*91f16700Schasinglulu #include <services/ffa_svc.h> 15*91f16700Schasinglulu #include "spmc.h" 16*91f16700Schasinglulu 17*91f16700Schasinglulu #include <platform_def.h> 18*91f16700Schasinglulu 19*91f16700Schasinglulu /******************************************************************************* 20*91f16700Schasinglulu * spmc_build_pm_message 21*91f16700Schasinglulu * 22*91f16700Schasinglulu * Builds an SPMC to SP direct message request. 23*91f16700Schasinglulu ******************************************************************************/ 24*91f16700Schasinglulu static void spmc_build_pm_message(gp_regs_t *gpregs, 25*91f16700Schasinglulu unsigned long long message, 26*91f16700Schasinglulu uint8_t pm_msg_type, 27*91f16700Schasinglulu uint16_t sp_id) 28*91f16700Schasinglulu { 29*91f16700Schasinglulu write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32); 30*91f16700Schasinglulu write_ctx_reg(gpregs, CTX_GPREG_X1, 31*91f16700Schasinglulu (FFA_SPMC_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) | 32*91f16700Schasinglulu sp_id); 33*91f16700Schasinglulu write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_FWK_MSG_BIT | 34*91f16700Schasinglulu (pm_msg_type & FFA_FWK_MSG_MASK)); 35*91f16700Schasinglulu write_ctx_reg(gpregs, CTX_GPREG_X3, message); 36*91f16700Schasinglulu } 37*91f16700Schasinglulu 38*91f16700Schasinglulu /******************************************************************************* 39*91f16700Schasinglulu * This CPU has been turned on. Enter the SP to initialise S-EL1. 40*91f16700Schasinglulu ******************************************************************************/ 41*91f16700Schasinglulu static void spmc_cpu_on_finish_handler(u_register_t unused) 42*91f16700Schasinglulu { 43*91f16700Schasinglulu struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); 44*91f16700Schasinglulu struct sp_exec_ctx *ec; 45*91f16700Schasinglulu unsigned int linear_id = plat_my_core_pos(); 46*91f16700Schasinglulu entry_point_info_t sec_ec_ep_info = {0}; 47*91f16700Schasinglulu uint64_t rc; 48*91f16700Schasinglulu 49*91f16700Schasinglulu /* Sanity check for a NULL pointer dereference. */ 50*91f16700Schasinglulu assert(sp != NULL); 51*91f16700Schasinglulu 52*91f16700Schasinglulu /* Initialize entry point information for the SP. */ 53*91f16700Schasinglulu SET_PARAM_HEAD(&sec_ec_ep_info, PARAM_EP, VERSION_1, 54*91f16700Schasinglulu SECURE | EP_ST_ENABLE); 55*91f16700Schasinglulu 56*91f16700Schasinglulu /* 57*91f16700Schasinglulu * Check if the primary execution context registered an entry point else 58*91f16700Schasinglulu * bail out early. 59*91f16700Schasinglulu * TODO: Add support for boot reason in manifest to allow jumping to 60*91f16700Schasinglulu * entrypoint into the primary execution context. 61*91f16700Schasinglulu */ 62*91f16700Schasinglulu if (sp->secondary_ep == 0) { 63*91f16700Schasinglulu WARN("%s: No secondary ep on core%u\n", __func__, linear_id); 64*91f16700Schasinglulu return; 65*91f16700Schasinglulu } 66*91f16700Schasinglulu 67*91f16700Schasinglulu sec_ec_ep_info.pc = sp->secondary_ep; 68*91f16700Schasinglulu 69*91f16700Schasinglulu /* 70*91f16700Schasinglulu * Setup and initialise the SP execution context on this physical cpu. 71*91f16700Schasinglulu */ 72*91f16700Schasinglulu spmc_el1_sp_setup(sp, &sec_ec_ep_info); 73*91f16700Schasinglulu spmc_sp_common_ep_commit(sp, &sec_ec_ep_info); 74*91f16700Schasinglulu 75*91f16700Schasinglulu /* Obtain a reference to the SP execution context. */ 76*91f16700Schasinglulu ec = spmc_get_sp_ec(sp); 77*91f16700Schasinglulu 78*91f16700Schasinglulu /* 79*91f16700Schasinglulu * TODO: Should we do some PM related state tracking of the SP execution 80*91f16700Schasinglulu * context here? 81*91f16700Schasinglulu */ 82*91f16700Schasinglulu 83*91f16700Schasinglulu /* Update the runtime model and state of the partition. */ 84*91f16700Schasinglulu ec->rt_model = RT_MODEL_INIT; 85*91f16700Schasinglulu ec->rt_state = RT_STATE_RUNNING; 86*91f16700Schasinglulu ec->dir_req_origin_id = INV_SP_ID; 87*91f16700Schasinglulu 88*91f16700Schasinglulu INFO("SP (0x%x) init start on core%u.\n", sp->sp_id, linear_id); 89*91f16700Schasinglulu 90*91f16700Schasinglulu rc = spmc_sp_synchronous_entry(ec); 91*91f16700Schasinglulu if (rc != 0ULL) { 92*91f16700Schasinglulu ERROR("%s failed (%lu) on CPU%u\n", __func__, rc, linear_id); 93*91f16700Schasinglulu } 94*91f16700Schasinglulu 95*91f16700Schasinglulu /* Update the runtime state of the partition. */ 96*91f16700Schasinglulu ec->rt_state = RT_STATE_WAITING; 97*91f16700Schasinglulu 98*91f16700Schasinglulu VERBOSE("CPU %u on!\n", linear_id); 99*91f16700Schasinglulu } 100*91f16700Schasinglulu /******************************************************************************* 101*91f16700Schasinglulu * Helper function to send a FF-A power management message to an SP. 102*91f16700Schasinglulu ******************************************************************************/ 103*91f16700Schasinglulu static int32_t spmc_send_pm_msg(uint8_t pm_msg_type, 104*91f16700Schasinglulu unsigned long long psci_event) 105*91f16700Schasinglulu { 106*91f16700Schasinglulu struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); 107*91f16700Schasinglulu struct sp_exec_ctx *ec; 108*91f16700Schasinglulu gp_regs_t *gpregs_ctx; 109*91f16700Schasinglulu unsigned int linear_id = plat_my_core_pos(); 110*91f16700Schasinglulu u_register_t resp; 111*91f16700Schasinglulu uint64_t rc; 112*91f16700Schasinglulu 113*91f16700Schasinglulu /* Obtain a reference to the SP execution context. */ 114*91f16700Schasinglulu ec = spmc_get_sp_ec(sp); 115*91f16700Schasinglulu 116*91f16700Schasinglulu /* 117*91f16700Schasinglulu * TODO: Should we do some PM related state tracking of the SP execution 118*91f16700Schasinglulu * context here? 119*91f16700Schasinglulu */ 120*91f16700Schasinglulu 121*91f16700Schasinglulu /* 122*91f16700Schasinglulu * Build an SPMC to SP direct message request. 123*91f16700Schasinglulu * Note that x4-x6 should be populated with the original PSCI arguments. 124*91f16700Schasinglulu */ 125*91f16700Schasinglulu spmc_build_pm_message(get_gpregs_ctx(&ec->cpu_ctx), 126*91f16700Schasinglulu psci_event, 127*91f16700Schasinglulu pm_msg_type, 128*91f16700Schasinglulu sp->sp_id); 129*91f16700Schasinglulu 130*91f16700Schasinglulu /* Sanity check partition state. */ 131*91f16700Schasinglulu assert(ec->rt_state == RT_STATE_WAITING); 132*91f16700Schasinglulu 133*91f16700Schasinglulu /* Update the runtime model and state of the partition. */ 134*91f16700Schasinglulu ec->rt_model = RT_MODEL_DIR_REQ; 135*91f16700Schasinglulu ec->rt_state = RT_STATE_RUNNING; 136*91f16700Schasinglulu ec->dir_req_origin_id = FFA_SPMC_ID; 137*91f16700Schasinglulu 138*91f16700Schasinglulu rc = spmc_sp_synchronous_entry(ec); 139*91f16700Schasinglulu if (rc != 0ULL) { 140*91f16700Schasinglulu ERROR("%s failed (%lu) on CPU%u.\n", __func__, rc, linear_id); 141*91f16700Schasinglulu assert(false); 142*91f16700Schasinglulu return -EINVAL; 143*91f16700Schasinglulu } 144*91f16700Schasinglulu 145*91f16700Schasinglulu /* 146*91f16700Schasinglulu * Validate we receive an expected response from the SP. 147*91f16700Schasinglulu * TODO: We don't currently support aborting an SP in the scenario 148*91f16700Schasinglulu * where it is misbehaving so assert these conditions are not 149*91f16700Schasinglulu * met for now. 150*91f16700Schasinglulu */ 151*91f16700Schasinglulu gpregs_ctx = get_gpregs_ctx(&ec->cpu_ctx); 152*91f16700Schasinglulu 153*91f16700Schasinglulu /* Expect a direct message response from the SP. */ 154*91f16700Schasinglulu resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X0); 155*91f16700Schasinglulu if (resp != FFA_MSG_SEND_DIRECT_RESP_SMC32) { 156*91f16700Schasinglulu ERROR("%s invalid SP response (%lx).\n", __func__, resp); 157*91f16700Schasinglulu assert(false); 158*91f16700Schasinglulu return -EINVAL; 159*91f16700Schasinglulu } 160*91f16700Schasinglulu 161*91f16700Schasinglulu /* Ensure the sender and receiver are populated correctly. */ 162*91f16700Schasinglulu resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X1); 163*91f16700Schasinglulu if (!(ffa_endpoint_source(resp) == sp->sp_id && 164*91f16700Schasinglulu ffa_endpoint_destination(resp) == FFA_SPMC_ID)) { 165*91f16700Schasinglulu ERROR("%s invalid src/dst response (%lx).\n", __func__, resp); 166*91f16700Schasinglulu assert(false); 167*91f16700Schasinglulu return -EINVAL; 168*91f16700Schasinglulu } 169*91f16700Schasinglulu 170*91f16700Schasinglulu /* Expect a PM message response from the SP. */ 171*91f16700Schasinglulu resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X2); 172*91f16700Schasinglulu if ((resp & FFA_FWK_MSG_BIT) == 0U || 173*91f16700Schasinglulu ((resp & FFA_FWK_MSG_MASK) != FFA_PM_MSG_PM_RESP)) { 174*91f16700Schasinglulu ERROR("%s invalid PM response (%lx).\n", __func__, resp); 175*91f16700Schasinglulu assert(false); 176*91f16700Schasinglulu return -EINVAL; 177*91f16700Schasinglulu } 178*91f16700Schasinglulu 179*91f16700Schasinglulu /* Update the runtime state of the partition. */ 180*91f16700Schasinglulu ec->rt_state = RT_STATE_WAITING; 181*91f16700Schasinglulu 182*91f16700Schasinglulu /* Return the status code returned by the SP */ 183*91f16700Schasinglulu return read_ctx_reg(gpregs_ctx, CTX_GPREG_X3); 184*91f16700Schasinglulu } 185*91f16700Schasinglulu 186*91f16700Schasinglulu /******************************************************************************* 187*91f16700Schasinglulu * spmc_cpu_suspend_finish_handler 188*91f16700Schasinglulu ******************************************************************************/ 189*91f16700Schasinglulu static void spmc_cpu_suspend_finish_handler(u_register_t unused) 190*91f16700Schasinglulu { 191*91f16700Schasinglulu struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); 192*91f16700Schasinglulu unsigned int linear_id = plat_my_core_pos(); 193*91f16700Schasinglulu int32_t rc; 194*91f16700Schasinglulu 195*91f16700Schasinglulu /* Sanity check for a NULL pointer dereference. */ 196*91f16700Schasinglulu assert(sp != NULL); 197*91f16700Schasinglulu 198*91f16700Schasinglulu /* 199*91f16700Schasinglulu * Check if the SP has subscribed for this power management message. 200*91f16700Schasinglulu * If not then we don't have anything else to do here. 201*91f16700Schasinglulu */ 202*91f16700Schasinglulu if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_SUSPEND_RESUME) == 0U) { 203*91f16700Schasinglulu goto exit; 204*91f16700Schasinglulu } 205*91f16700Schasinglulu 206*91f16700Schasinglulu rc = spmc_send_pm_msg(FFA_PM_MSG_WB_REQ, FFA_WB_TYPE_NOTS2RAM); 207*91f16700Schasinglulu if (rc < 0) { 208*91f16700Schasinglulu ERROR("%s failed (%d) on CPU%u\n", __func__, rc, linear_id); 209*91f16700Schasinglulu return; 210*91f16700Schasinglulu } 211*91f16700Schasinglulu 212*91f16700Schasinglulu exit: 213*91f16700Schasinglulu VERBOSE("CPU %u resumed!\n", linear_id); 214*91f16700Schasinglulu } 215*91f16700Schasinglulu 216*91f16700Schasinglulu /******************************************************************************* 217*91f16700Schasinglulu * spmc_cpu_suspend_handler 218*91f16700Schasinglulu ******************************************************************************/ 219*91f16700Schasinglulu static void spmc_cpu_suspend_handler(u_register_t unused) 220*91f16700Schasinglulu { 221*91f16700Schasinglulu struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); 222*91f16700Schasinglulu unsigned int linear_id = plat_my_core_pos(); 223*91f16700Schasinglulu int32_t rc; 224*91f16700Schasinglulu 225*91f16700Schasinglulu /* Sanity check for a NULL pointer dereference. */ 226*91f16700Schasinglulu assert(sp != NULL); 227*91f16700Schasinglulu 228*91f16700Schasinglulu /* 229*91f16700Schasinglulu * Check if the SP has subscribed for this power management message. 230*91f16700Schasinglulu * If not then we don't have anything else to do here. 231*91f16700Schasinglulu */ 232*91f16700Schasinglulu if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_SUSPEND) == 0U) { 233*91f16700Schasinglulu goto exit; 234*91f16700Schasinglulu } 235*91f16700Schasinglulu 236*91f16700Schasinglulu rc = spmc_send_pm_msg(FFA_FWK_MSG_PSCI, PSCI_CPU_SUSPEND_AARCH64); 237*91f16700Schasinglulu if (rc < 0) { 238*91f16700Schasinglulu ERROR("%s failed (%d) on CPU%u\n", __func__, rc, linear_id); 239*91f16700Schasinglulu return; 240*91f16700Schasinglulu } 241*91f16700Schasinglulu exit: 242*91f16700Schasinglulu VERBOSE("CPU %u suspend!\n", linear_id); 243*91f16700Schasinglulu } 244*91f16700Schasinglulu 245*91f16700Schasinglulu /******************************************************************************* 246*91f16700Schasinglulu * spmc_cpu_off_handler 247*91f16700Schasinglulu ******************************************************************************/ 248*91f16700Schasinglulu static int32_t spmc_cpu_off_handler(u_register_t unused) 249*91f16700Schasinglulu { 250*91f16700Schasinglulu struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); 251*91f16700Schasinglulu unsigned int linear_id = plat_my_core_pos(); 252*91f16700Schasinglulu int32_t ret = 0; 253*91f16700Schasinglulu 254*91f16700Schasinglulu /* Sanity check for a NULL pointer dereference. */ 255*91f16700Schasinglulu assert(sp != NULL); 256*91f16700Schasinglulu 257*91f16700Schasinglulu /* 258*91f16700Schasinglulu * Check if the SP has subscribed for this power management message. 259*91f16700Schasinglulu * If not then we don't have anything else to do here. 260*91f16700Schasinglulu */ 261*91f16700Schasinglulu if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_OFF) == 0U) { 262*91f16700Schasinglulu goto exit; 263*91f16700Schasinglulu } 264*91f16700Schasinglulu 265*91f16700Schasinglulu ret = spmc_send_pm_msg(FFA_FWK_MSG_PSCI, PSCI_CPU_OFF); 266*91f16700Schasinglulu if (ret < 0) { 267*91f16700Schasinglulu ERROR("%s failed (%d) on CPU%u\n", __func__, ret, linear_id); 268*91f16700Schasinglulu return ret; 269*91f16700Schasinglulu } 270*91f16700Schasinglulu 271*91f16700Schasinglulu exit: 272*91f16700Schasinglulu VERBOSE("CPU %u off!\n", linear_id); 273*91f16700Schasinglulu return ret; 274*91f16700Schasinglulu } 275*91f16700Schasinglulu 276*91f16700Schasinglulu /******************************************************************************* 277*91f16700Schasinglulu * Structure populated by the SPM Core to perform any bookkeeping before 278*91f16700Schasinglulu * PSCI executes a power mgmt. operation. 279*91f16700Schasinglulu ******************************************************************************/ 280*91f16700Schasinglulu const spd_pm_ops_t spmc_pm = { 281*91f16700Schasinglulu .svc_on_finish = spmc_cpu_on_finish_handler, 282*91f16700Schasinglulu .svc_off = spmc_cpu_off_handler, 283*91f16700Schasinglulu .svc_suspend = spmc_cpu_suspend_handler, 284*91f16700Schasinglulu .svc_suspend_finish = spmc_cpu_suspend_finish_handler 285*91f16700Schasinglulu }; 286