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