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; |
| 86 | |
| 87 | INFO("SP (0x%x) init start on core%u.\n", sp->sp_id, linear_id); |
| 88 | |
| 89 | rc = spmc_sp_synchronous_entry(ec); |
| 90 | if (rc != 0ULL) { |
| 91 | ERROR("%s failed (%lu) on CPU%u\n", __func__, rc, linear_id); |
| 92 | } |
| 93 | |
| 94 | /* Update the runtime state of the partition. */ |
| 95 | ec->rt_state = RT_STATE_WAITING; |
| 96 | |
| 97 | VERBOSE("CPU %u on!\n", linear_id); |
| 98 | } |
| 99 | /******************************************************************************* |
| 100 | * Helper function to send a FF-A power management message to an SP. |
| 101 | ******************************************************************************/ |
| 102 | static int32_t spmc_send_pm_msg(uint8_t pm_msg_type, |
| 103 | unsigned long long psci_event) |
| 104 | { |
| 105 | struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); |
| 106 | struct sp_exec_ctx *ec; |
| 107 | gp_regs_t *gpregs_ctx; |
| 108 | unsigned int linear_id = plat_my_core_pos(); |
| 109 | u_register_t resp; |
| 110 | uint64_t rc; |
| 111 | |
| 112 | /* Obtain a reference to the SP execution context. */ |
| 113 | ec = spmc_get_sp_ec(sp); |
| 114 | |
| 115 | /* |
| 116 | * TODO: Should we do some PM related state tracking of the SP execution |
| 117 | * context here? |
| 118 | */ |
| 119 | |
| 120 | /* |
| 121 | * Build an SPMC to SP direct message request. |
| 122 | * Note that x4-x6 should be populated with the original PSCI arguments. |
| 123 | */ |
| 124 | spmc_build_pm_message(get_gpregs_ctx(&ec->cpu_ctx), |
| 125 | psci_event, |
| 126 | pm_msg_type, |
| 127 | sp->sp_id); |
| 128 | |
| 129 | /* Sanity check partition state. */ |
| 130 | assert(ec->rt_state == RT_STATE_WAITING); |
| 131 | |
| 132 | /* Update the runtime model and state of the partition. */ |
| 133 | ec->rt_model = RT_MODEL_DIR_REQ; |
| 134 | ec->rt_state = RT_STATE_RUNNING; |
| 135 | |
| 136 | rc = spmc_sp_synchronous_entry(ec); |
| 137 | if (rc != 0ULL) { |
| 138 | ERROR("%s failed (%lu) on CPU%u.\n", __func__, rc, linear_id); |
| 139 | assert(false); |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Validate we receive an expected response from the SP. |
| 145 | * TODO: We don't currently support aborting an SP in the scenario |
| 146 | * where it is misbehaving so assert these conditions are not |
| 147 | * met for now. |
| 148 | */ |
| 149 | gpregs_ctx = get_gpregs_ctx(&ec->cpu_ctx); |
| 150 | |
| 151 | /* Expect a direct message response from the SP. */ |
| 152 | resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X0); |
| 153 | if (resp != FFA_MSG_SEND_DIRECT_RESP_SMC32) { |
| 154 | ERROR("%s invalid SP response (%lx).\n", __func__, resp); |
| 155 | assert(false); |
| 156 | return -EINVAL; |
| 157 | } |
| 158 | |
| 159 | /* Ensure the sender and receiver are populated correctly. */ |
| 160 | resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X1); |
| 161 | if (!(ffa_endpoint_source(resp) == sp->sp_id && |
| 162 | ffa_endpoint_destination(resp) == FFA_SPMC_ID)) { |
| 163 | ERROR("%s invalid src/dst response (%lx).\n", __func__, resp); |
| 164 | assert(false); |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
| 168 | /* Expect a PM message response from the SP. */ |
| 169 | resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X2); |
| 170 | if ((resp & FFA_FWK_MSG_BIT) == 0U || |
| 171 | ((resp & FFA_FWK_MSG_MASK) != FFA_PM_MSG_PM_RESP)) { |
| 172 | ERROR("%s invalid PM response (%lx).\n", __func__, resp); |
| 173 | assert(false); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | /* Update the runtime state of the partition. */ |
| 178 | ec->rt_state = RT_STATE_WAITING; |
| 179 | |
| 180 | /* Return the status code returned by the SP */ |
| 181 | return read_ctx_reg(gpregs_ctx, CTX_GPREG_X3); |
| 182 | } |
| 183 | |
| 184 | /******************************************************************************* |
| 185 | * spmc_cpu_suspend_finish_handler |
| 186 | ******************************************************************************/ |
| 187 | static void spmc_cpu_suspend_finish_handler(u_register_t unused) |
| 188 | { |
| 189 | struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); |
| 190 | unsigned int linear_id = plat_my_core_pos(); |
| 191 | int32_t rc; |
| 192 | |
| 193 | /* Sanity check for a NULL pointer dereference. */ |
| 194 | assert(sp != NULL); |
| 195 | |
| 196 | /* |
| 197 | * Check if the SP has subscribed for this power management message. |
| 198 | * If not then we don't have anything else to do here. |
| 199 | */ |
| 200 | if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_SUSPEND_RESUME) == 0U) { |
| 201 | goto exit; |
| 202 | } |
| 203 | |
| 204 | rc = spmc_send_pm_msg(FFA_PM_MSG_WB_REQ, FFA_WB_TYPE_NOTS2RAM); |
| 205 | if (rc < 0) { |
| 206 | ERROR("%s failed (%d) on CPU%u\n", __func__, rc, linear_id); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | exit: |
| 211 | VERBOSE("CPU %u resumed!\n", linear_id); |
| 212 | } |
| 213 | |
| 214 | /******************************************************************************* |
| 215 | * spmc_cpu_suspend_handler |
| 216 | ******************************************************************************/ |
| 217 | static void spmc_cpu_suspend_handler(u_register_t unused) |
| 218 | { |
| 219 | struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); |
| 220 | unsigned int linear_id = plat_my_core_pos(); |
| 221 | int32_t rc; |
| 222 | |
| 223 | /* Sanity check for a NULL pointer dereference. */ |
| 224 | assert(sp != NULL); |
| 225 | |
| 226 | /* |
| 227 | * Check if the SP has subscribed for this power management message. |
| 228 | * If not then we don't have anything else to do here. |
| 229 | */ |
| 230 | if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_SUSPEND) == 0U) { |
| 231 | goto exit; |
| 232 | } |
| 233 | |
| 234 | rc = spmc_send_pm_msg(FFA_FWK_MSG_PSCI, PSCI_CPU_SUSPEND_AARCH64); |
| 235 | if (rc < 0) { |
| 236 | ERROR("%s failed (%d) on CPU%u\n", __func__, rc, linear_id); |
| 237 | return; |
| 238 | } |
| 239 | exit: |
| 240 | VERBOSE("CPU %u suspend!\n", linear_id); |
| 241 | } |
| 242 | |
| 243 | /******************************************************************************* |
| 244 | * spmc_cpu_off_handler |
| 245 | ******************************************************************************/ |
| 246 | static int32_t spmc_cpu_off_handler(u_register_t unused) |
| 247 | { |
| 248 | struct secure_partition_desc *sp = spmc_get_current_sp_ctx(); |
| 249 | unsigned int linear_id = plat_my_core_pos(); |
| 250 | int32_t ret = 0; |
| 251 | |
| 252 | /* Sanity check for a NULL pointer dereference. */ |
| 253 | assert(sp != NULL); |
| 254 | |
| 255 | /* |
| 256 | * Check if the SP has subscribed for this power management message. |
| 257 | * If not then we don't have anything else to do here. |
| 258 | */ |
| 259 | if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_OFF) == 0U) { |
| 260 | goto exit; |
| 261 | } |
| 262 | |
| 263 | ret = spmc_send_pm_msg(FFA_FWK_MSG_PSCI, PSCI_CPU_OFF); |
| 264 | if (ret < 0) { |
| 265 | ERROR("%s failed (%d) on CPU%u\n", __func__, ret, linear_id); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | exit: |
| 270 | VERBOSE("CPU %u off!\n", linear_id); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | /******************************************************************************* |
| 275 | * Structure populated by the SPM Core to perform any bookkeeping before |
| 276 | * PSCI executes a power mgmt. operation. |
| 277 | ******************************************************************************/ |
| 278 | const spd_pm_ops_t spmc_pm = { |
| 279 | .svc_on_finish = spmc_cpu_on_finish_handler, |
| 280 | .svc_off = spmc_cpu_off_handler, |
| 281 | .svc_suspend = spmc_cpu_suspend_handler, |
| 282 | .svc_suspend_finish = spmc_cpu_suspend_finish_handler |
| 283 | }; |