Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 1 | /* |
Roberto Vargas | 2b36b15 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <css_def.h> |
| 10 | #include <css_pm.h> |
| 11 | #include <debug.h> |
| 12 | #include <plat_arm.h> |
| 13 | #include <platform.h> |
| 14 | #include <string.h> |
| 15 | #include "../scmi/scmi.h" |
| 16 | #include "css_scp.h" |
| 17 | |
| 18 | /* |
| 19 | * This file implements the SCP helper functions using SCMI protocol. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * SCMI power state parameter bit field encoding for ARM CSS platforms. |
| 24 | * |
| 25 | * 31 20 19 16 15 12 11 8 7 4 3 0 |
| 26 | * +-------------------------------------------------------------+ |
| 27 | * | SBZ | Max level | Level 3 | Level 2 | Level 1 | Level 0 | |
| 28 | * | | | state | state | state | state | |
| 29 | * +-------------------------------------------------------------+ |
| 30 | * |
| 31 | * `Max level` encodes the highest level that has a valid power state |
| 32 | * encoded in the power state. |
| 33 | */ |
| 34 | #define SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT 16 |
| 35 | #define SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH 4 |
| 36 | #define SCMI_PWR_STATE_MAX_PWR_LVL_MASK \ |
| 37 | ((1 << SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH) - 1) |
| 38 | #define SCMI_SET_PWR_STATE_MAX_PWR_LVL(pwr_state, max_lvl) \ |
| 39 | (pwr_state) |= ((max_lvl) & SCMI_PWR_STATE_MAX_PWR_LVL_MASK) \ |
| 40 | << SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT |
| 41 | #define SCMI_GET_PWR_STATE_MAX_PWR_LVL(pwr_state) \ |
| 42 | (((pwr_state) >> SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT) \ |
| 43 | & SCMI_PWR_STATE_MAX_PWR_LVL_MASK) |
| 44 | |
| 45 | #define SCMI_PWR_STATE_LVL_WIDTH 4 |
| 46 | #define SCMI_PWR_STATE_LVL_MASK \ |
| 47 | ((1 << SCMI_PWR_STATE_LVL_WIDTH) - 1) |
| 48 | #define SCMI_SET_PWR_STATE_LVL(pwr_state, lvl, lvl_state) \ |
| 49 | (pwr_state) |= ((lvl_state) & SCMI_PWR_STATE_LVL_MASK) \ |
| 50 | << (SCMI_PWR_STATE_LVL_WIDTH * (lvl)) |
| 51 | #define SCMI_GET_PWR_STATE_LVL(pwr_state, lvl) \ |
| 52 | (((pwr_state) >> (SCMI_PWR_STATE_LVL_WIDTH * (lvl))) & \ |
| 53 | SCMI_PWR_STATE_LVL_MASK) |
| 54 | |
| 55 | /* |
| 56 | * The SCMI power state enumeration for a power domain level |
| 57 | */ |
| 58 | typedef enum { |
| 59 | scmi_power_state_off = 0, |
| 60 | scmi_power_state_on = 1, |
| 61 | scmi_power_state_sleep = 2, |
| 62 | } scmi_power_state_t; |
| 63 | |
| 64 | /* |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 65 | * The global handle for invoking the SCMI driver APIs after the driver |
| 66 | * has been initialized. |
| 67 | */ |
Roberto Vargas | 2b36b15 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 68 | static void *scmi_handle; |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 69 | |
| 70 | /* The SCMI channel global object */ |
| 71 | static scmi_channel_t scmi_channel; |
| 72 | |
Jeenu Viswambharan | 749d25b | 2017-08-23 14:12:59 +0100 | [diff] [blame] | 73 | ARM_INSTANTIATE_LOCK; |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Helper function to suspend a CPU power domain and its parent power domains |
| 77 | * if applicable. |
| 78 | */ |
| 79 | void css_scp_suspend(const psci_power_state_t *target_state) |
| 80 | { |
| 81 | int lvl, ret; |
| 82 | uint32_t scmi_pwr_state = 0; |
| 83 | |
| 84 | /* At least power domain level 0 should be specified to be suspended */ |
| 85 | assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == |
| 86 | ARM_LOCAL_STATE_OFF); |
| 87 | |
| 88 | /* Check if power down at system power domain level is requested */ |
| 89 | if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) { |
| 90 | /* Issue SCMI command for SYSTEM_SUSPEND */ |
| 91 | ret = scmi_sys_pwr_state_set(scmi_handle, |
| 92 | SCMI_SYS_PWR_FORCEFUL_REQ, |
| 93 | SCMI_SYS_PWR_SUSPEND); |
| 94 | if (ret != SCMI_E_SUCCESS) { |
| 95 | ERROR("SCMI system power domain suspend return 0x%x unexpected\n", |
| 96 | ret); |
| 97 | panic(); |
| 98 | } |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * If we reach here, then assert that power down at system power domain |
| 104 | * level is running. |
| 105 | */ |
| 106 | assert(target_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] == |
| 107 | ARM_LOCAL_STATE_RUN); |
| 108 | |
| 109 | /* For level 0, specify `scmi_power_state_sleep` as the power state */ |
| 110 | SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, ARM_PWR_LVL0, |
| 111 | scmi_power_state_sleep); |
| 112 | |
| 113 | for (lvl = ARM_PWR_LVL1; lvl <= PLAT_MAX_PWR_LVL; lvl++) { |
| 114 | if (target_state->pwr_domain_state[lvl] == ARM_LOCAL_STATE_RUN) |
| 115 | break; |
| 116 | |
| 117 | assert(target_state->pwr_domain_state[lvl] == |
| 118 | ARM_LOCAL_STATE_OFF); |
| 119 | /* |
| 120 | * Specify `scmi_power_state_off` as power state for higher |
| 121 | * levels. |
| 122 | */ |
| 123 | SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl, |
| 124 | scmi_power_state_off); |
| 125 | } |
| 126 | |
| 127 | SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1); |
| 128 | |
| 129 | ret = scmi_pwr_state_set(scmi_handle, |
| 130 | plat_css_core_pos_to_scmi_dmn_id_map[plat_my_core_pos()], |
| 131 | scmi_pwr_state); |
| 132 | |
| 133 | if (ret != SCMI_E_SUCCESS) { |
| 134 | ERROR("SCMI set power state command return 0x%x unexpected\n", |
| 135 | ret); |
| 136 | panic(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Helper function to turn off a CPU power domain and its parent power domains |
| 142 | * if applicable. |
| 143 | */ |
| 144 | void css_scp_off(const psci_power_state_t *target_state) |
| 145 | { |
| 146 | int lvl = 0, ret; |
| 147 | uint32_t scmi_pwr_state = 0; |
| 148 | |
| 149 | /* At-least the CPU level should be specified to be OFF */ |
| 150 | assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == |
| 151 | ARM_LOCAL_STATE_OFF); |
| 152 | |
| 153 | /* PSCI CPU OFF cannot be used to turn OFF system power domain */ |
| 154 | assert(target_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] == |
| 155 | ARM_LOCAL_STATE_RUN); |
| 156 | |
| 157 | for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) { |
| 158 | if (target_state->pwr_domain_state[lvl] == ARM_LOCAL_STATE_RUN) |
| 159 | break; |
| 160 | |
| 161 | assert(target_state->pwr_domain_state[lvl] == |
| 162 | ARM_LOCAL_STATE_OFF); |
| 163 | SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl, |
| 164 | scmi_power_state_off); |
| 165 | } |
| 166 | |
| 167 | SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1); |
| 168 | |
| 169 | ret = scmi_pwr_state_set(scmi_handle, |
| 170 | plat_css_core_pos_to_scmi_dmn_id_map[plat_my_core_pos()], |
| 171 | scmi_pwr_state); |
| 172 | |
| 173 | if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) { |
| 174 | ERROR("SCMI set power state command return 0x%x unexpected\n", |
| 175 | ret); |
| 176 | panic(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Helper function to turn ON a CPU power domain and its parent power domains |
| 182 | * if applicable. |
| 183 | */ |
| 184 | void css_scp_on(u_register_t mpidr) |
| 185 | { |
Soby Mathew | e089e3f | 2017-06-09 15:04:43 +0100 | [diff] [blame] | 186 | int lvl = 0, ret, core_pos; |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 187 | uint32_t scmi_pwr_state = 0; |
| 188 | |
| 189 | for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) |
| 190 | SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl, |
| 191 | scmi_power_state_on); |
| 192 | |
| 193 | SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1); |
| 194 | |
Soby Mathew | e089e3f | 2017-06-09 15:04:43 +0100 | [diff] [blame] | 195 | core_pos = plat_core_pos_by_mpidr(mpidr); |
| 196 | assert(core_pos >= 0 && core_pos < PLATFORM_CORE_COUNT); |
| 197 | |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 198 | ret = scmi_pwr_state_set(scmi_handle, |
Soby Mathew | e089e3f | 2017-06-09 15:04:43 +0100 | [diff] [blame] | 199 | plat_css_core_pos_to_scmi_dmn_id_map[core_pos], |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 200 | scmi_pwr_state); |
| 201 | |
| 202 | if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) { |
| 203 | ERROR("SCMI set power state command return 0x%x unexpected\n", |
| 204 | ret); |
| 205 | panic(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Helper function to get the power state of a power domain node as reported |
| 211 | * by the SCP. |
| 212 | */ |
| 213 | int css_scp_get_power_state(u_register_t mpidr, unsigned int power_level) |
| 214 | { |
| 215 | int ret, cpu_idx; |
| 216 | uint32_t scmi_pwr_state = 0, lvl_state; |
| 217 | |
| 218 | /* We don't support get power state at the system power domain level */ |
| 219 | if ((power_level > PLAT_MAX_PWR_LVL) || |
| 220 | (power_level == CSS_SYSTEM_PWR_DMN_LVL)) { |
| 221 | WARN("Invalid power level %u specified for SCMI get power state\n", |
| 222 | power_level); |
| 223 | return PSCI_E_INVALID_PARAMS; |
| 224 | } |
| 225 | |
| 226 | cpu_idx = plat_core_pos_by_mpidr(mpidr); |
| 227 | assert(cpu_idx > -1); |
| 228 | |
| 229 | ret = scmi_pwr_state_get(scmi_handle, |
| 230 | plat_css_core_pos_to_scmi_dmn_id_map[cpu_idx], |
| 231 | &scmi_pwr_state); |
| 232 | |
| 233 | if (ret != SCMI_E_SUCCESS) { |
| 234 | WARN("SCMI get power state command return 0x%x unexpected\n", |
| 235 | ret); |
| 236 | return PSCI_E_INVALID_PARAMS; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Find the maximum power level described in the get power state |
| 241 | * command. If it is less than the requested power level, then assume |
| 242 | * the requested power level is ON. |
| 243 | */ |
| 244 | if (SCMI_GET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state) < power_level) |
| 245 | return HW_ON; |
| 246 | |
| 247 | lvl_state = SCMI_GET_PWR_STATE_LVL(scmi_pwr_state, power_level); |
| 248 | if (lvl_state == scmi_power_state_on) |
| 249 | return HW_ON; |
| 250 | |
| 251 | assert((lvl_state == scmi_power_state_off) || |
| 252 | (lvl_state == scmi_power_state_sleep)); |
| 253 | return HW_OFF; |
| 254 | } |
| 255 | |
Roberto Vargas | fc2b4eb | 2017-07-31 09:45:10 +0100 | [diff] [blame] | 256 | void __dead2 css_scp_system_off(int state) |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 257 | { |
| 258 | int ret; |
| 259 | |
| 260 | /* |
| 261 | * Disable GIC CPU interface to prevent pending interrupt from waking |
| 262 | * up the AP from WFI. |
| 263 | */ |
| 264 | plat_arm_gic_cpuif_disable(); |
| 265 | |
| 266 | /* |
Roberto Vargas | fc2b4eb | 2017-07-31 09:45:10 +0100 | [diff] [blame] | 267 | * Issue SCMI command. First issue a graceful |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 268 | * request and if that fails force the request. |
| 269 | */ |
| 270 | ret = scmi_sys_pwr_state_set(scmi_handle, |
| 271 | SCMI_SYS_PWR_FORCEFUL_REQ, |
Roberto Vargas | fc2b4eb | 2017-07-31 09:45:10 +0100 | [diff] [blame] | 272 | state); |
| 273 | |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 274 | if (ret != SCMI_E_SUCCESS) { |
Roberto Vargas | fc2b4eb | 2017-07-31 09:45:10 +0100 | [diff] [blame] | 275 | ERROR("SCMI system power state set 0x%x returns unexpected 0x%x\n", |
| 276 | state, ret); |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 277 | panic(); |
| 278 | } |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 279 | wfi(); |
Roberto Vargas | fc2b4eb | 2017-07-31 09:45:10 +0100 | [diff] [blame] | 280 | ERROR("CSS set power state: operation not handled.\n"); |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 281 | panic(); |
| 282 | } |
| 283 | |
| 284 | /* |
Roberto Vargas | fc2b4eb | 2017-07-31 09:45:10 +0100 | [diff] [blame] | 285 | * Helper function to shutdown the system via SCMI. |
| 286 | */ |
| 287 | void __dead2 css_scp_sys_shutdown(void) |
| 288 | { |
| 289 | css_scp_system_off(SCMI_SYS_PWR_SHUTDOWN); |
| 290 | } |
| 291 | |
| 292 | /* |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 293 | * Helper function to reset the system via SCMI. |
| 294 | */ |
| 295 | void __dead2 css_scp_sys_reboot(void) |
| 296 | { |
Roberto Vargas | fc2b4eb | 2017-07-31 09:45:10 +0100 | [diff] [blame] | 297 | css_scp_system_off(SCMI_SYS_PWR_COLD_RESET); |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | scmi_channel_plat_info_t plat_css_scmi_plat_info = { |
| 301 | .scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE, |
| 302 | .db_reg_addr = PLAT_CSS_MHU_BASE + CSS_SCMI_MHU_DB_REG_OFF, |
Soby Mathew | 1ced6b8 | 2017-06-12 12:37:10 +0100 | [diff] [blame] | 303 | .db_preserve_mask = 0xfffffffe, |
| 304 | .db_modify_mask = 0x1, |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | void plat_arm_pwrc_setup(void) |
| 308 | { |
| 309 | scmi_channel.info = &plat_css_scmi_plat_info; |
| 310 | scmi_channel.lock = ARM_LOCK_GET_INSTANCE; |
| 311 | scmi_handle = scmi_init(&scmi_channel); |
| 312 | if (scmi_handle == NULL) { |
| 313 | ERROR("SCMI Initialization failed\n"); |
| 314 | panic(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /****************************************************************************** |
| 319 | * This function overrides the default definition for ARM platforms. Initialize |
| 320 | * the SCMI driver, query capability via SCMI and modify the PSCI capability |
| 321 | * based on that. |
| 322 | *****************************************************************************/ |
| 323 | const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops) |
| 324 | { |
| 325 | uint32_t msg_attr; |
| 326 | int ret; |
| 327 | |
| 328 | assert(scmi_handle); |
| 329 | |
| 330 | /* Check that power domain POWER_STATE_SET message is supported */ |
| 331 | ret = scmi_proto_msg_attr(scmi_handle, SCMI_PWR_DMN_PROTO_ID, |
| 332 | SCMI_PWR_STATE_SET_MSG, &msg_attr); |
| 333 | if (ret != SCMI_E_SUCCESS) { |
| 334 | ERROR("Set power state command is not supported by SCMI\n"); |
| 335 | panic(); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Don't support PSCI NODE_HW_STATE call if SCMI doesn't support |
| 340 | * POWER_STATE_GET message. |
| 341 | */ |
| 342 | ret = scmi_proto_msg_attr(scmi_handle, SCMI_PWR_DMN_PROTO_ID, |
| 343 | SCMI_PWR_STATE_GET_MSG, &msg_attr); |
| 344 | if (ret != SCMI_E_SUCCESS) |
| 345 | ops->get_node_hw_state = NULL; |
| 346 | |
| 347 | /* Check if the SCMI SYSTEM_POWER_STATE_SET message is supported */ |
| 348 | ret = scmi_proto_msg_attr(scmi_handle, SCMI_SYS_PWR_PROTO_ID, |
| 349 | SCMI_SYS_PWR_STATE_SET_MSG, &msg_attr); |
| 350 | if (ret != SCMI_E_SUCCESS) { |
| 351 | /* System power management operations are not supported */ |
| 352 | ops->system_off = NULL; |
| 353 | ops->system_reset = NULL; |
| 354 | ops->get_sys_suspend_power_state = NULL; |
Roberto Vargas | 3caafd7 | 2017-08-16 08:57:45 +0100 | [diff] [blame] | 355 | } else { |
| 356 | if (!(msg_attr & SCMI_SYS_PWR_SUSPEND_SUPPORTED)) { |
| 357 | /* |
| 358 | * System power management protocol is available, but |
| 359 | * it does not support SYSTEM SUSPEND. |
| 360 | */ |
| 361 | ops->get_sys_suspend_power_state = NULL; |
| 362 | } |
| 363 | if (!(msg_attr & SCMI_SYS_PWR_WARM_RESET_SUPPORTED)) { |
| 364 | /* |
| 365 | * WARM reset is not available. |
| 366 | */ |
| 367 | ops->system_reset2 = NULL; |
| 368 | } |
Soby Mathew | ea26bad | 2016-11-14 12:25:45 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return ops; |
| 372 | } |
Roberto Vargas | 3caafd7 | 2017-08-16 08:57:45 +0100 | [diff] [blame] | 373 | |
| 374 | int css_system_reset2(int is_vendor, int reset_type, u_register_t cookie) |
| 375 | { |
| 376 | if (is_vendor || (reset_type != PSCI_RESET2_SYSTEM_WARM_RESET)) |
| 377 | return PSCI_E_INVALID_PARAMS; |
| 378 | |
| 379 | css_scp_system_off(SCMI_SYS_PWR_WARM_RESET); |
| 380 | /* |
| 381 | * css_scp_system_off cannot return (it is a __dead function), |
| 382 | * but css_system_reset2 has to return some value, even in |
| 383 | * this case. |
| 384 | */ |
| 385 | return 0; |
| 386 | } |