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