1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2015 ARM Limited 5 */ 6 7 #define pr_fmt(fmt) "psci: " fmt 8 9 #include <linux/acpi.h> 10 #include <linux/arm-smccc.h> 11 #include <linux/cpuidle.h> 12 #include <linux/errno.h> 13 #include <linux/linkage.h> 14 #include <linux/of.h> 15 #include <linux/pm.h> 16 #include <linux/printk.h> 17 #include <linux/psci.h> 18 #include <linux/reboot.h> 19 #include <linux/slab.h> 20 #include <linux/suspend.h> 21 22 #include <uapi/linux/psci.h> 23 24 #include <asm/cpuidle.h> 25 #include <asm/cputype.h> 26 #include <asm/system_misc.h> 27 #include <asm/smp_plat.h> 28 #include <asm/suspend.h> 29 30 /* 31 * While a 64-bit OS can make calls with SMC32 calling conventions, for some 32 * calls it is necessary to use SMC64 to pass or return 64-bit values. 33 * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate 34 * (native-width) function ID. 35 */ 36 #ifdef CONFIG_64BIT 37 #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name 38 #else 39 #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name 40 #endif 41 42 /* 43 * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF 44 * calls to its resident CPU, so we must avoid issuing those. We never migrate 45 * a Trusted OS even if it claims to be capable of migration -- doing so will 46 * require cooperation with a Trusted OS driver. 47 */ 48 static int resident_cpu = -1; 49 struct psci_operations psci_ops; 50 static enum arm_smccc_conduit psci_conduit = SMCCC_CONDUIT_NONE; 51 52 bool psci_tos_resident_on(int cpu) 53 { 54 return cpu == resident_cpu; 55 } 56 57 typedef unsigned long (psci_fn)(unsigned long, unsigned long, 58 unsigned long, unsigned long); 59 static psci_fn *invoke_psci_fn; 60 61 enum psci_function { 62 PSCI_FN_CPU_SUSPEND, 63 PSCI_FN_CPU_ON, 64 PSCI_FN_CPU_OFF, 65 PSCI_FN_MIGRATE, 66 PSCI_FN_MAX, 67 }; 68 69 static u32 psci_function_id[PSCI_FN_MAX]; 70 71 #define PSCI_0_2_POWER_STATE_MASK \ 72 (PSCI_0_2_POWER_STATE_ID_MASK | \ 73 PSCI_0_2_POWER_STATE_TYPE_MASK | \ 74 PSCI_0_2_POWER_STATE_AFFL_MASK) 75 76 #define PSCI_1_0_EXT_POWER_STATE_MASK \ 77 (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \ 78 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK) 79 80 static u32 psci_cpu_suspend_feature; 81 static bool psci_system_reset2_supported; 82 void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit); 83 84 static inline bool psci_has_ext_power_state(void) 85 { 86 return psci_cpu_suspend_feature & 87 PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK; 88 } 89 90 bool psci_has_osi_support(void) 91 { 92 return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED; 93 } 94 95 static inline bool psci_power_state_loses_context(u32 state) 96 { 97 const u32 mask = psci_has_ext_power_state() ? 98 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK : 99 PSCI_0_2_POWER_STATE_TYPE_MASK; 100 101 return state & mask; 102 } 103 104 bool psci_power_state_is_valid(u32 state) 105 { 106 const u32 valid_mask = psci_has_ext_power_state() ? 107 PSCI_1_0_EXT_POWER_STATE_MASK : 108 PSCI_0_2_POWER_STATE_MASK; 109 110 return !(state & ~valid_mask); 111 } 112 113 static unsigned long __invoke_psci_fn_hvc(unsigned long function_id, 114 unsigned long arg0, unsigned long arg1, 115 unsigned long arg2) 116 { 117 struct arm_smccc_res res; 118 119 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); 120 return res.a0; 121 } 122 123 static unsigned long __invoke_psci_fn_smc(unsigned long function_id, 124 unsigned long arg0, unsigned long arg1, 125 unsigned long arg2) 126 { 127 struct arm_smccc_res res; 128 129 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); 130 return res.a0; 131 } 132 133 static int psci_to_linux_errno(int errno) 134 { 135 switch (errno) { 136 case PSCI_RET_SUCCESS: 137 return 0; 138 case PSCI_RET_NOT_SUPPORTED: 139 return -EOPNOTSUPP; 140 case PSCI_RET_INVALID_PARAMS: 141 case PSCI_RET_INVALID_ADDRESS: 142 return -EINVAL; 143 case PSCI_RET_DENIED: 144 return -EPERM; 145 }; 146 147 return -EINVAL; 148 } 149 150 static u32 psci_get_version(void) 151 { 152 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0); 153 } 154 155 int psci_set_osi_mode(void) 156 { 157 int err; 158 159 err = invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, 160 PSCI_1_0_SUSPEND_MODE_OSI, 0, 0); 161 return psci_to_linux_errno(err); 162 } 163 164 static int psci_cpu_suspend(u32 state, unsigned long entry_point) 165 { 166 int err; 167 u32 fn; 168 169 fn = psci_function_id[PSCI_FN_CPU_SUSPEND]; 170 err = invoke_psci_fn(fn, state, entry_point, 0); 171 return psci_to_linux_errno(err); 172 } 173 174 static int psci_cpu_off(u32 state) 175 { 176 int err; 177 u32 fn; 178 179 fn = psci_function_id[PSCI_FN_CPU_OFF]; 180 err = invoke_psci_fn(fn, state, 0, 0); 181 return psci_to_linux_errno(err); 182 } 183 184 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point) 185 { 186 int err; 187 u32 fn; 188 189 fn = psci_function_id[PSCI_FN_CPU_ON]; 190 err = invoke_psci_fn(fn, cpuid, entry_point, 0); 191 return psci_to_linux_errno(err); 192 } 193 194 static int psci_migrate(unsigned long cpuid) 195 { 196 int err; 197 u32 fn; 198 199 fn = psci_function_id[PSCI_FN_MIGRATE]; 200 err = invoke_psci_fn(fn, cpuid, 0, 0); 201 return psci_to_linux_errno(err); 202 } 203 204 static int psci_affinity_info(unsigned long target_affinity, 205 unsigned long lowest_affinity_level) 206 { 207 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO), 208 target_affinity, lowest_affinity_level, 0); 209 } 210 211 static int psci_migrate_info_type(void) 212 { 213 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0); 214 } 215 216 static unsigned long psci_migrate_info_up_cpu(void) 217 { 218 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU), 219 0, 0, 0); 220 } 221 222 static void set_conduit(enum arm_smccc_conduit conduit) 223 { 224 switch (conduit) { 225 case SMCCC_CONDUIT_HVC: 226 invoke_psci_fn = __invoke_psci_fn_hvc; 227 break; 228 case SMCCC_CONDUIT_SMC: 229 invoke_psci_fn = __invoke_psci_fn_smc; 230 break; 231 default: 232 WARN(1, "Unexpected PSCI conduit %d\n", conduit); 233 } 234 235 psci_conduit = conduit; 236 } 237 238 static int get_set_conduit_method(struct device_node *np) 239 { 240 const char *method; 241 242 pr_info("probing for conduit method from DT.\n"); 243 244 if (of_property_read_string(np, "method", &method)) { 245 pr_warn("missing \"method\" property\n"); 246 return -ENXIO; 247 } 248 249 if (!strcmp("hvc", method)) { 250 set_conduit(SMCCC_CONDUIT_HVC); 251 } else if (!strcmp("smc", method)) { 252 set_conduit(SMCCC_CONDUIT_SMC); 253 } else { 254 pr_warn("invalid \"method\" property: %s\n", method); 255 return -EINVAL; 256 } 257 return 0; 258 } 259 260 static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd) 261 { 262 if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) && 263 psci_system_reset2_supported) { 264 /* 265 * reset_type[31] = 0 (architectural) 266 * reset_type[30:0] = 0 (SYSTEM_WARM_RESET) 267 * cookie = 0 (ignored by the implementation) 268 */ 269 invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0); 270 } else { 271 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); 272 } 273 } 274 275 static void psci_sys_poweroff(void) 276 { 277 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); 278 } 279 280 static int __init psci_features(u32 psci_func_id) 281 { 282 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES, 283 psci_func_id, 0, 0); 284 } 285 286 #ifdef CONFIG_CPU_IDLE 287 static int psci_suspend_finisher(unsigned long state) 288 { 289 u32 power_state = state; 290 291 return psci_ops.cpu_suspend(power_state, __pa_symbol(cpu_resume)); 292 } 293 294 int psci_cpu_suspend_enter(u32 state) 295 { 296 int ret; 297 298 if (!psci_power_state_loses_context(state)) 299 ret = psci_ops.cpu_suspend(state, 0); 300 else 301 ret = cpu_suspend(state, psci_suspend_finisher); 302 303 return ret; 304 } 305 #endif 306 307 static int psci_system_suspend(unsigned long unused) 308 { 309 return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND), 310 __pa_symbol(cpu_resume), 0, 0); 311 } 312 313 static int psci_system_suspend_enter(suspend_state_t state) 314 { 315 return cpu_suspend(0, psci_system_suspend); 316 } 317 318 static const struct platform_suspend_ops psci_suspend_ops = { 319 .valid = suspend_valid_only_mem, 320 .enter = psci_system_suspend_enter, 321 }; 322 323 static void __init psci_init_system_reset2(void) 324 { 325 int ret; 326 327 ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2)); 328 329 if (ret != PSCI_RET_NOT_SUPPORTED) 330 psci_system_reset2_supported = true; 331 } 332 333 static void __init psci_init_system_suspend(void) 334 { 335 int ret; 336 337 if (!IS_ENABLED(CONFIG_SUSPEND)) 338 return; 339 340 ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND)); 341 342 if (ret != PSCI_RET_NOT_SUPPORTED) 343 suspend_set_ops(&psci_suspend_ops); 344 } 345 346 static void __init psci_init_cpu_suspend(void) 347 { 348 int feature = psci_features(psci_function_id[PSCI_FN_CPU_SUSPEND]); 349 350 if (feature != PSCI_RET_NOT_SUPPORTED) 351 psci_cpu_suspend_feature = feature; 352 } 353 354 /* 355 * Detect the presence of a resident Trusted OS which may cause CPU_OFF to 356 * return DENIED (which would be fatal). 357 */ 358 static void __init psci_init_migrate(void) 359 { 360 unsigned long cpuid; 361 int type, cpu = -1; 362 363 type = psci_ops.migrate_info_type(); 364 365 if (type == PSCI_0_2_TOS_MP) { 366 pr_info("Trusted OS migration not required\n"); 367 return; 368 } 369 370 if (type == PSCI_RET_NOT_SUPPORTED) { 371 pr_info("MIGRATE_INFO_TYPE not supported.\n"); 372 return; 373 } 374 375 if (type != PSCI_0_2_TOS_UP_MIGRATE && 376 type != PSCI_0_2_TOS_UP_NO_MIGRATE) { 377 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type); 378 return; 379 } 380 381 cpuid = psci_migrate_info_up_cpu(); 382 if (cpuid & ~MPIDR_HWID_BITMASK) { 383 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n", 384 cpuid); 385 return; 386 } 387 388 cpu = get_logical_index(cpuid); 389 resident_cpu = cpu >= 0 ? cpu : -1; 390 391 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid); 392 } 393 394 static void __init psci_init_smccc(void) 395 { 396 u32 ver = ARM_SMCCC_VERSION_1_0; 397 int feature; 398 399 feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID); 400 401 if (feature != PSCI_RET_NOT_SUPPORTED) { 402 u32 ret; 403 ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0); 404 if (ret >= ARM_SMCCC_VERSION_1_1) { 405 arm_smccc_version_init(ret, psci_conduit); 406 ver = ret; 407 } 408 } 409 410 /* 411 * Conveniently, the SMCCC and PSCI versions are encoded the 412 * same way. No, this isn't accidental. 413 */ 414 pr_info("SMC Calling Convention v%d.%d\n", 415 PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver)); 416 417 } 418 419 static void __init psci_0_2_set_functions(void) 420 { 421 pr_info("Using standard PSCI v0.2 function IDs\n"); 422 psci_ops.get_version = psci_get_version; 423 424 psci_function_id[PSCI_FN_CPU_SUSPEND] = 425 PSCI_FN_NATIVE(0_2, CPU_SUSPEND); 426 psci_ops.cpu_suspend = psci_cpu_suspend; 427 428 psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF; 429 psci_ops.cpu_off = psci_cpu_off; 430 431 psci_function_id[PSCI_FN_CPU_ON] = PSCI_FN_NATIVE(0_2, CPU_ON); 432 psci_ops.cpu_on = psci_cpu_on; 433 434 psci_function_id[PSCI_FN_MIGRATE] = PSCI_FN_NATIVE(0_2, MIGRATE); 435 psci_ops.migrate = psci_migrate; 436 437 psci_ops.affinity_info = psci_affinity_info; 438 439 psci_ops.migrate_info_type = psci_migrate_info_type; 440 441 arm_pm_restart = psci_sys_reset; 442 443 pm_power_off = psci_sys_poweroff; 444 } 445 446 /* 447 * Probe function for PSCI firmware versions >= 0.2 448 */ 449 static int __init psci_probe(void) 450 { 451 u32 ver = psci_get_version(); 452 453 pr_info("PSCIv%d.%d detected in firmware.\n", 454 PSCI_VERSION_MAJOR(ver), 455 PSCI_VERSION_MINOR(ver)); 456 457 if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) { 458 pr_err("Conflicting PSCI version detected.\n"); 459 return -EINVAL; 460 } 461 462 psci_0_2_set_functions(); 463 464 psci_init_migrate(); 465 466 if (PSCI_VERSION_MAJOR(ver) >= 1) { 467 psci_init_smccc(); 468 psci_init_cpu_suspend(); 469 psci_init_system_suspend(); 470 psci_init_system_reset2(); 471 } 472 473 return 0; 474 } 475 476 typedef int (*psci_initcall_t)(const struct device_node *); 477 478 /* 479 * PSCI init function for PSCI versions >=0.2 480 * 481 * Probe based on PSCI PSCI_VERSION function 482 */ 483 static int __init psci_0_2_init(struct device_node *np) 484 { 485 int err; 486 487 err = get_set_conduit_method(np); 488 if (err) 489 return err; 490 491 /* 492 * Starting with v0.2, the PSCI specification introduced a call 493 * (PSCI_VERSION) that allows probing the firmware version, so 494 * that PSCI function IDs and version specific initialization 495 * can be carried out according to the specific version reported 496 * by firmware 497 */ 498 return psci_probe(); 499 } 500 501 /* 502 * PSCI < v0.2 get PSCI Function IDs via DT. 503 */ 504 static int __init psci_0_1_init(struct device_node *np) 505 { 506 u32 id; 507 int err; 508 509 err = get_set_conduit_method(np); 510 if (err) 511 return err; 512 513 pr_info("Using PSCI v0.1 Function IDs from DT\n"); 514 515 if (!of_property_read_u32(np, "cpu_suspend", &id)) { 516 psci_function_id[PSCI_FN_CPU_SUSPEND] = id; 517 psci_ops.cpu_suspend = psci_cpu_suspend; 518 } 519 520 if (!of_property_read_u32(np, "cpu_off", &id)) { 521 psci_function_id[PSCI_FN_CPU_OFF] = id; 522 psci_ops.cpu_off = psci_cpu_off; 523 } 524 525 if (!of_property_read_u32(np, "cpu_on", &id)) { 526 psci_function_id[PSCI_FN_CPU_ON] = id; 527 psci_ops.cpu_on = psci_cpu_on; 528 } 529 530 if (!of_property_read_u32(np, "migrate", &id)) { 531 psci_function_id[PSCI_FN_MIGRATE] = id; 532 psci_ops.migrate = psci_migrate; 533 } 534 535 return 0; 536 } 537 538 static int __init psci_1_0_init(struct device_node *np) 539 { 540 int err; 541 542 err = psci_0_2_init(np); 543 if (err) 544 return err; 545 546 if (psci_has_osi_support()) { 547 pr_info("OSI mode supported.\n"); 548 549 /* Default to PC mode. */ 550 invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, 551 PSCI_1_0_SUSPEND_MODE_PC, 0, 0); 552 } 553 554 return 0; 555 } 556 557 static const struct of_device_id psci_of_match[] __initconst = { 558 { .compatible = "arm,psci", .data = psci_0_1_init}, 559 { .compatible = "arm,psci-0.2", .data = psci_0_2_init}, 560 { .compatible = "arm,psci-1.0", .data = psci_1_0_init}, 561 {}, 562 }; 563 564 int __init psci_dt_init(void) 565 { 566 struct device_node *np; 567 const struct of_device_id *matched_np; 568 psci_initcall_t init_fn; 569 int ret; 570 571 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np); 572 573 if (!np || !of_device_is_available(np)) 574 return -ENODEV; 575 576 init_fn = (psci_initcall_t)matched_np->data; 577 ret = init_fn(np); 578 579 of_node_put(np); 580 return ret; 581 } 582 583 #ifdef CONFIG_ACPI 584 /* 585 * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's 586 * explicitly clarified in SBBR 587 */ 588 int __init psci_acpi_init(void) 589 { 590 if (!acpi_psci_present()) { 591 pr_info("is not implemented in ACPI.\n"); 592 return -EOPNOTSUPP; 593 } 594 595 pr_info("probing for conduit method from ACPI.\n"); 596 597 if (acpi_psci_use_hvc()) 598 set_conduit(SMCCC_CONDUIT_HVC); 599 else 600 set_conduit(SMCCC_CONDUIT_SMC); 601 602 return psci_probe(); 603 } 604 #endif 605