1 /* 2 * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <string.h> 9 10 #include <arch.h> 11 #include <arch_features.h> 12 #include <arch_helpers.h> 13 #include <common/debug.h> 14 #include <lib/pmf/pmf.h> 15 #include <lib/runtime_instr.h> 16 #include <lib/smccc.h> 17 #include <plat/common/platform.h> 18 #include <services/arm_arch_svc.h> 19 20 #include "psci_private.h" 21 22 /******************************************************************************* 23 * PSCI frontend api for servicing SMCs. Described in the PSCI spec. 24 ******************************************************************************/ 25 int psci_cpu_on(u_register_t target_cpu, 26 uintptr_t entrypoint, 27 u_register_t context_id) 28 29 { 30 int rc; 31 entry_point_info_t ep; 32 33 /* Validate the target CPU */ 34 if (!is_valid_mpidr(target_cpu)) 35 return PSCI_E_INVALID_PARAMS; 36 37 /* Validate the entry point and get the entry_point_info */ 38 rc = psci_validate_entry_point(&ep, entrypoint, context_id); 39 if (rc != PSCI_E_SUCCESS) 40 return rc; 41 42 /* 43 * To turn this cpu on, specify which power 44 * levels need to be turned on 45 */ 46 return psci_cpu_on_start(target_cpu, &ep); 47 } 48 49 unsigned int psci_version(void) 50 { 51 return PSCI_MAJOR_VER | PSCI_MINOR_VER; 52 } 53 54 int psci_cpu_suspend(unsigned int power_state, 55 uintptr_t entrypoint, 56 u_register_t context_id) 57 { 58 int rc; 59 unsigned int target_pwrlvl, is_power_down_state; 60 entry_point_info_t ep; 61 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} }; 62 plat_local_state_t cpu_pd_state; 63 unsigned int cpu_idx = plat_my_core_pos(); 64 #if PSCI_OS_INIT_MODE 65 plat_local_state_t prev[PLAT_MAX_PWR_LVL]; 66 #endif 67 68 #if ERRATA_SME_POWER_DOWN 69 /* 70 * If SME isn't off, attempting a real power down will only end up being 71 * rejected. If we got called with SME on, fall back to a normal 72 * suspend. We can't force SME off as in the event the power down is 73 * rejected for another reason (eg GIC) we'd lose the SME context. 74 */ 75 if (is_feat_sme_supported() && read_svcr() != 0) { 76 power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT); 77 power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT); 78 } 79 #endif /* ERRATA_SME_POWER_DOWN */ 80 81 /* Validate the power_state parameter */ 82 rc = psci_validate_power_state(power_state, &state_info); 83 if (rc != PSCI_E_SUCCESS) { 84 assert(rc == PSCI_E_INVALID_PARAMS); 85 return rc; 86 } 87 88 /* 89 * Get the value of the state type bit from the power state parameter. 90 */ 91 is_power_down_state = psci_get_pstate_type(power_state); 92 93 /* Sanity check the requested suspend levels */ 94 assert(psci_validate_suspend_req(&state_info, is_power_down_state) 95 == PSCI_E_SUCCESS); 96 97 target_pwrlvl = psci_find_target_suspend_lvl(&state_info); 98 if (target_pwrlvl == PSCI_INVALID_PWR_LVL) { 99 ERROR("Invalid target power level for suspend operation\n"); 100 panic(); 101 } 102 103 /* Fast path for CPU standby.*/ 104 if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) { 105 if (psci_plat_pm_ops->cpu_standby == NULL) 106 return PSCI_E_INVALID_PARAMS; 107 108 /* 109 * Set the state of the CPU power domain to the platform 110 * specific retention state and enter the standby state. 111 */ 112 cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL]; 113 psci_set_cpu_local_state(cpu_pd_state); 114 115 #if PSCI_OS_INIT_MODE 116 /* 117 * If in OS-initiated mode, save a copy of the previous 118 * requested local power states and update the new requested 119 * local power states for this CPU. 120 */ 121 if (psci_suspend_mode == OS_INIT) { 122 psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx, 123 &state_info, prev); 124 } 125 #endif 126 127 #if ENABLE_PSCI_STAT 128 plat_psci_stat_accounting_start(&state_info); 129 #endif 130 131 #if ENABLE_RUNTIME_INSTRUMENTATION 132 PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 133 RT_INSTR_ENTER_HW_LOW_PWR, 134 PMF_NO_CACHE_MAINT); 135 #endif 136 137 psci_plat_pm_ops->cpu_standby(cpu_pd_state); 138 139 /* Upon exit from standby, set the state back to RUN. */ 140 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN); 141 142 #if PSCI_OS_INIT_MODE 143 /* 144 * If in OS-initiated mode, restore the previous requested 145 * local power states for this CPU. 146 */ 147 if (psci_suspend_mode == OS_INIT) { 148 psci_restore_req_local_pwr_states(cpu_idx, prev); 149 } 150 #endif 151 152 #if ENABLE_RUNTIME_INSTRUMENTATION 153 PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 154 RT_INSTR_EXIT_HW_LOW_PWR, 155 PMF_NO_CACHE_MAINT); 156 #endif 157 158 #if ENABLE_PSCI_STAT 159 plat_psci_stat_accounting_stop(&state_info); 160 161 /* Update PSCI stats */ 162 psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info); 163 #endif 164 165 return PSCI_E_SUCCESS; 166 } 167 168 /* 169 * If a power down state has been requested, we need to verify entry 170 * point and program entry information. 171 */ 172 if (is_power_down_state != 0U) { 173 rc = psci_validate_entry_point(&ep, entrypoint, context_id); 174 if (rc != PSCI_E_SUCCESS) 175 return rc; 176 } 177 178 /* 179 * Do what is needed to enter the power down state. Upon success, 180 * enter the final wfi which will power down this CPU. This function 181 * might return if the power down was abandoned for any reason, e.g. 182 * arrival of an interrupt 183 */ 184 rc = psci_cpu_suspend_start(cpu_idx, 185 &ep, 186 target_pwrlvl, 187 &state_info, 188 is_power_down_state); 189 190 return rc; 191 } 192 193 194 int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id) 195 { 196 int rc; 197 psci_power_state_t state_info; 198 entry_point_info_t ep; 199 unsigned int cpu_idx = plat_my_core_pos(); 200 201 /* Check if the current CPU is the last ON CPU in the system */ 202 if (!psci_is_last_on_cpu(cpu_idx)) 203 return PSCI_E_DENIED; 204 205 /* Validate the entry point and get the entry_point_info */ 206 rc = psci_validate_entry_point(&ep, entrypoint, context_id); 207 if (rc != PSCI_E_SUCCESS) 208 return rc; 209 210 /* Query the psci_power_state for system suspend */ 211 psci_query_sys_suspend_pwrstate(&state_info); 212 213 /* 214 * Check if platform allows suspend to Highest power level 215 * (System level) 216 */ 217 if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) 218 return PSCI_E_DENIED; 219 220 /* Ensure that the psci_power_state makes sense */ 221 assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN) 222 == PSCI_E_SUCCESS); 223 assert(is_local_state_off( 224 state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0); 225 226 /* 227 * Do what is needed to enter the system suspend state. This function 228 * might return if the power down was abandoned for any reason, e.g. 229 * arrival of an interrupt 230 */ 231 rc = psci_cpu_suspend_start(cpu_idx, 232 &ep, 233 PLAT_MAX_PWR_LVL, 234 &state_info, 235 PSTATE_TYPE_POWERDOWN); 236 237 return rc; 238 } 239 240 int psci_cpu_off(void) 241 { 242 int rc; 243 unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL; 244 245 /* 246 * Do what is needed to power off this CPU and possible higher power 247 * levels if it able to do so. Upon success, enter the final wfi 248 * which will power down this CPU. 249 */ 250 rc = psci_do_cpu_off(target_pwrlvl); 251 252 /* 253 * The only error cpu_off can return is E_DENIED. So check if that's 254 * indeed the case. 255 */ 256 assert(rc == PSCI_E_DENIED); 257 258 return rc; 259 } 260 261 int psci_affinity_info(u_register_t target_affinity, 262 unsigned int lowest_affinity_level) 263 { 264 unsigned int target_idx; 265 266 /* Validate the target affinity */ 267 if (!is_valid_mpidr(target_affinity)) 268 return PSCI_E_INVALID_PARAMS; 269 270 /* We dont support level higher than PSCI_CPU_PWR_LVL */ 271 if (lowest_affinity_level > PSCI_CPU_PWR_LVL) 272 return PSCI_E_INVALID_PARAMS; 273 274 /* Calculate the cpu index of the target */ 275 target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity); 276 277 /* 278 * Generic management: 279 * Perform cache maintanence ahead of reading the target CPU state to 280 * ensure that the data is not stale. 281 * There is a theoretical edge case where the cache may contain stale 282 * data for the target CPU data - this can occur under the following 283 * conditions: 284 * - the target CPU is in another cluster from the current 285 * - the target CPU was the last CPU to shutdown on its cluster 286 * - the cluster was removed from coherency as part of the CPU shutdown 287 * 288 * In this case the cache maintenace that was performed as part of the 289 * target CPUs shutdown was not seen by the current CPU's cluster. And 290 * so the cache may contain stale data for the target CPU. 291 */ 292 flush_cpu_data_by_index(target_idx, 293 psci_svc_cpu_data.aff_info_state); 294 295 return psci_get_aff_info_state_by_idx(target_idx); 296 } 297 298 int psci_migrate(u_register_t target_cpu) 299 { 300 int rc; 301 u_register_t resident_cpu_mpidr; 302 303 /* Validate the target cpu */ 304 if (!is_valid_mpidr(target_cpu)) 305 return PSCI_E_INVALID_PARAMS; 306 307 rc = psci_spd_migrate_info(&resident_cpu_mpidr); 308 if (rc != PSCI_TOS_UP_MIG_CAP) 309 return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ? 310 PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED; 311 312 /* 313 * Migrate should only be invoked on the CPU where 314 * the Secure OS is resident. 315 */ 316 if (resident_cpu_mpidr != read_mpidr_el1()) 317 return PSCI_E_NOT_PRESENT; 318 319 /* Check the validity of the specified target cpu */ 320 if (!is_valid_mpidr(target_cpu)) 321 return PSCI_E_INVALID_PARAMS; 322 323 assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL)); 324 325 rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu); 326 assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL)); 327 328 return rc; 329 } 330 331 int psci_migrate_info_type(void) 332 { 333 u_register_t resident_cpu_mpidr; 334 335 return psci_spd_migrate_info(&resident_cpu_mpidr); 336 } 337 338 u_register_t psci_migrate_info_up_cpu(void) 339 { 340 u_register_t resident_cpu_mpidr; 341 int rc; 342 343 /* 344 * Return value of this depends upon what 345 * psci_spd_migrate_info() returns. 346 */ 347 rc = psci_spd_migrate_info(&resident_cpu_mpidr); 348 if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP)) 349 return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS; 350 351 return resident_cpu_mpidr; 352 } 353 354 int psci_node_hw_state(u_register_t target_cpu, 355 unsigned int power_level) 356 { 357 int rc; 358 359 /* Validate target_cpu */ 360 if (!is_valid_mpidr(target_cpu)) 361 return PSCI_E_INVALID_PARAMS; 362 363 /* Validate power_level against PLAT_MAX_PWR_LVL */ 364 if (power_level > PLAT_MAX_PWR_LVL) 365 return PSCI_E_INVALID_PARAMS; 366 367 /* 368 * Dispatch this call to platform to query power controller, and pass on 369 * to the caller what it returns 370 */ 371 assert(psci_plat_pm_ops->get_node_hw_state != NULL); 372 rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level); 373 assert(((rc >= HW_ON) && (rc <= HW_STANDBY)) 374 || (rc == PSCI_E_NOT_SUPPORTED) 375 || (rc == PSCI_E_INVALID_PARAMS)); 376 return rc; 377 } 378 379 int psci_features(unsigned int psci_fid) 380 { 381 unsigned int local_caps = psci_caps; 382 383 if (psci_fid == SMCCC_VERSION) 384 return PSCI_E_SUCCESS; 385 386 /* Check if it is a 64 bit function */ 387 if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) 388 local_caps &= PSCI_CAP_64BIT_MASK; 389 390 /* Check for invalid fid */ 391 if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid) 392 && is_psci_fid(psci_fid))) 393 return PSCI_E_NOT_SUPPORTED; 394 395 396 /* Check if the psci fid is supported or not */ 397 if ((local_caps & define_psci_cap(psci_fid)) == 0U) 398 return PSCI_E_NOT_SUPPORTED; 399 400 /* Format the feature flags */ 401 if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) || 402 (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) { 403 unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) | 404 (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT)); 405 return (int)ret; 406 } 407 408 /* Return 0 for all other fid's */ 409 return PSCI_E_SUCCESS; 410 } 411 412 #if PSCI_OS_INIT_MODE 413 int psci_set_suspend_mode(unsigned int mode) 414 { 415 if (psci_suspend_mode == mode) { 416 return PSCI_E_SUCCESS; 417 } 418 419 unsigned int this_core = plat_my_core_pos(); 420 421 if (mode == PLAT_COORD) { 422 /* Check if the current CPU is the last ON CPU in the system */ 423 if (!psci_is_last_on_cpu_safe(this_core)) { 424 return PSCI_E_DENIED; 425 } 426 } 427 428 if (mode == OS_INIT) { 429 /* 430 * Check if all CPUs in the system are ON or if the current 431 * CPU is the last ON CPU in the system. 432 */ 433 if (!(psci_are_all_cpus_on_safe(this_core) || 434 psci_is_last_on_cpu_safe(this_core))) { 435 return PSCI_E_DENIED; 436 } 437 } 438 439 psci_suspend_mode = mode; 440 psci_flush_dcache_range((uintptr_t)&psci_suspend_mode, 441 sizeof(psci_suspend_mode)); 442 443 return PSCI_E_SUCCESS; 444 } 445 #endif 446 447 /******************************************************************************* 448 * PSCI top level handler for servicing SMCs. 449 ******************************************************************************/ 450 u_register_t psci_smc_handler(uint32_t smc_fid, 451 u_register_t x1, 452 u_register_t x2, 453 u_register_t x3, 454 u_register_t x4, 455 void *cookie, 456 void *handle, 457 u_register_t flags) 458 { 459 u_register_t ret; 460 461 if (is_caller_secure(flags)) 462 return (u_register_t)SMC_UNK; 463 464 /* Check the fid against the capabilities */ 465 if ((psci_caps & define_psci_cap(smc_fid)) == 0U) 466 return (u_register_t)SMC_UNK; 467 468 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) { 469 /* 32-bit PSCI function, clear top parameter bits */ 470 471 uint32_t r1 = (uint32_t)x1; 472 uint32_t r2 = (uint32_t)x2; 473 uint32_t r3 = (uint32_t)x3; 474 475 switch (smc_fid) { 476 case PSCI_VERSION: 477 ret = (u_register_t)psci_version(); 478 break; 479 480 case PSCI_CPU_OFF: 481 ret = (u_register_t)psci_cpu_off(); 482 break; 483 484 case PSCI_CPU_SUSPEND_AARCH32: 485 ret = (u_register_t)psci_cpu_suspend(r1, r2, r3); 486 break; 487 488 case PSCI_CPU_ON_AARCH32: 489 ret = (u_register_t)psci_cpu_on(r1, r2, r3); 490 break; 491 492 case PSCI_AFFINITY_INFO_AARCH32: 493 ret = (u_register_t)psci_affinity_info(r1, r2); 494 break; 495 496 case PSCI_MIG_AARCH32: 497 ret = (u_register_t)psci_migrate(r1); 498 break; 499 500 case PSCI_MIG_INFO_TYPE: 501 ret = (u_register_t)psci_migrate_info_type(); 502 break; 503 504 case PSCI_MIG_INFO_UP_CPU_AARCH32: 505 ret = psci_migrate_info_up_cpu(); 506 break; 507 508 case PSCI_NODE_HW_STATE_AARCH32: 509 ret = (u_register_t)psci_node_hw_state(r1, r2); 510 break; 511 512 case PSCI_SYSTEM_SUSPEND_AARCH32: 513 ret = (u_register_t)psci_system_suspend(r1, r2); 514 break; 515 516 case PSCI_SYSTEM_OFF: 517 psci_system_off(); 518 /* We should never return from psci_system_off() */ 519 break; 520 521 case PSCI_SYSTEM_RESET: 522 psci_system_reset(); 523 /* We should never return from psci_system_reset() */ 524 break; 525 526 case PSCI_FEATURES: 527 ret = (u_register_t)psci_features(r1); 528 break; 529 530 #if PSCI_OS_INIT_MODE 531 case PSCI_SET_SUSPEND_MODE: 532 ret = (u_register_t)psci_set_suspend_mode(r1); 533 break; 534 #endif 535 536 #if ENABLE_PSCI_STAT 537 case PSCI_STAT_RESIDENCY_AARCH32: 538 ret = psci_stat_residency(r1, r2); 539 break; 540 541 case PSCI_STAT_COUNT_AARCH32: 542 ret = psci_stat_count(r1, r2); 543 break; 544 #endif 545 case PSCI_MEM_PROTECT: 546 ret = psci_mem_protect(r1); 547 break; 548 549 case PSCI_MEM_CHK_RANGE_AARCH32: 550 ret = psci_mem_chk_range(r1, r2); 551 break; 552 553 case PSCI_SYSTEM_RESET2_AARCH32: 554 /* We should never return from psci_system_reset2() */ 555 ret = psci_system_reset2(r1, r2); 556 break; 557 558 default: 559 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 560 ret = (u_register_t)SMC_UNK; 561 break; 562 } 563 } else { 564 /* 64-bit PSCI function */ 565 566 switch (smc_fid) { 567 case PSCI_CPU_SUSPEND_AARCH64: 568 ret = (u_register_t) 569 psci_cpu_suspend((unsigned int)x1, x2, x3); 570 break; 571 572 case PSCI_CPU_ON_AARCH64: 573 ret = (u_register_t)psci_cpu_on(x1, x2, x3); 574 break; 575 576 case PSCI_AFFINITY_INFO_AARCH64: 577 ret = (u_register_t) 578 psci_affinity_info(x1, (unsigned int)x2); 579 break; 580 581 case PSCI_MIG_AARCH64: 582 ret = (u_register_t)psci_migrate(x1); 583 break; 584 585 case PSCI_MIG_INFO_UP_CPU_AARCH64: 586 ret = psci_migrate_info_up_cpu(); 587 break; 588 589 case PSCI_NODE_HW_STATE_AARCH64: 590 ret = (u_register_t)psci_node_hw_state( 591 x1, (unsigned int) x2); 592 break; 593 594 case PSCI_SYSTEM_SUSPEND_AARCH64: 595 ret = (u_register_t)psci_system_suspend(x1, x2); 596 break; 597 598 #if ENABLE_PSCI_STAT 599 case PSCI_STAT_RESIDENCY_AARCH64: 600 ret = psci_stat_residency(x1, (unsigned int) x2); 601 break; 602 603 case PSCI_STAT_COUNT_AARCH64: 604 ret = psci_stat_count(x1, (unsigned int) x2); 605 break; 606 #endif 607 608 case PSCI_MEM_CHK_RANGE_AARCH64: 609 ret = psci_mem_chk_range(x1, x2); 610 break; 611 612 case PSCI_SYSTEM_RESET2_AARCH64: 613 /* We should never return from psci_system_reset2() */ 614 ret = psci_system_reset2((uint32_t) x1, x2); 615 break; 616 617 default: 618 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 619 ret = (u_register_t)SMC_UNK; 620 break; 621 } 622 } 623 624 return ret; 625 } 626