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