Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

arm64: Use SYSTEM_OFF2 PSCI call to power off for hibernate

The PSCI v1.3 specification adds support for a SYSTEM_OFF2 function
which is analogous to ACPI S4 state. This will allow hosting
environments to determine that a guest is hibernated rather than just
powered off, and handle that state appropriately on subsequent launches.

Since commit 60c0d45a7f7a ("efi/arm64: use UEFI for system reset and
poweroff") the EFI shutdown method is deliberately preferred over PSCI
or other methods. So register a SYS_OFF_MODE_POWER_OFF handler which
*only* handles the hibernation, leaving the original PSCI SYSTEM_OFF as
a last resort via the legacy pm_power_off function pointer.

The hibernation code already exports a system_entering_hibernation()
function which is be used by the higher-priority handler to check for
hibernation. That existing function just returns the value of a static
boolean variable from hibernate.c, which was previously only set in the
hibernation_platform_enter() code path. Set the same flag in the simpler
code path around the call to kernel_power_off() too.

An alternative way to hook SYSTEM_OFF2 into the hibernation code would
be to register a platform_hibernation_ops structure with an ->enter()
method which makes the new SYSTEM_OFF2 call. But that would have the
unwanted side-effect of making hibernation take a completely different
code path in hibernation_platform_enter(), invoking a lot of special dpm
callbacks.

Another option might be to add a new SYS_OFF_MODE_HIBERNATE mode, with
fallback to SYS_OFF_MODE_POWER_OFF. Or to use the sys_off_data to
indicate whether the power off is for hibernation.

But this version works and is relatively simple.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Acked-by: Rafael J. Wysocki <rafael@kernel.org>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Link: https://lore.kernel.org/r/20241019172459.2241939-7-dwmw2@infradead.org
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>

authored by

David Woodhouse and committed by
Oliver Upton
3e251afa 94f985c3

+49 -1
+45
drivers/firmware/psci/psci.c
··· 78 78 79 79 static u32 psci_cpu_suspend_feature; 80 80 static bool psci_system_reset2_supported; 81 + static bool psci_system_off2_hibernate_supported; 81 82 82 83 static inline bool psci_has_ext_power_state(void) 83 84 { ··· 334 333 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); 335 334 } 336 335 336 + #ifdef CONFIG_HIBERNATION 337 + static int psci_sys_hibernate(struct sys_off_data *data) 338 + { 339 + /* 340 + * If no hibernate type is specified SYSTEM_OFF2 defaults to selecting 341 + * HIBERNATE_OFF. 342 + * 343 + * There are hypervisors in the wild that do not align with the spec and 344 + * reject calls that explicitly provide a hibernate type. For 345 + * compatibility with these nonstandard implementations, pass 0 as the 346 + * type. 347 + */ 348 + if (system_entering_hibernation()) 349 + invoke_psci_fn(PSCI_FN_NATIVE(1_3, SYSTEM_OFF2), 0, 0, 0); 350 + return NOTIFY_DONE; 351 + } 352 + 353 + static int __init psci_hibernate_init(void) 354 + { 355 + if (psci_system_off2_hibernate_supported) { 356 + /* Higher priority than EFI shutdown, but only for hibernate */ 357 + register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, 358 + SYS_OFF_PRIO_FIRMWARE + 2, 359 + psci_sys_hibernate, NULL); 360 + } 361 + return 0; 362 + } 363 + subsys_initcall(psci_hibernate_init); 364 + #endif 365 + 337 366 static int psci_features(u32 psci_func_id) 338 367 { 339 368 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES, ··· 395 364 PSCI_ID_NATIVE(1_1, SYSTEM_RESET2), 396 365 PSCI_ID(1_1, MEM_PROTECT), 397 366 PSCI_ID_NATIVE(1_1, MEM_PROTECT_CHECK_RANGE), 367 + PSCI_ID_NATIVE(1_3, SYSTEM_OFF2), 398 368 }; 399 369 400 370 static int psci_debugfs_read(struct seq_file *s, void *data) ··· 557 525 psci_system_reset2_supported = true; 558 526 } 559 527 528 + static void __init psci_init_system_off2(void) 529 + { 530 + int ret; 531 + 532 + ret = psci_features(PSCI_FN_NATIVE(1_3, SYSTEM_OFF2)); 533 + if (ret < 0) 534 + return; 535 + 536 + if (ret & PSCI_1_3_OFF_TYPE_HIBERNATE_OFF) 537 + psci_system_off2_hibernate_supported = true; 538 + } 539 + 560 540 static void __init psci_init_system_suspend(void) 561 541 { 562 542 int ret; ··· 699 655 psci_init_cpu_suspend(); 700 656 psci_init_system_suspend(); 701 657 psci_init_system_reset2(); 658 + psci_init_system_off2(); 702 659 kvm_init_hyp_services(); 703 660 } 704 661
+4 -1
kernel/power/hibernate.c
··· 685 685 } 686 686 fallthrough; 687 687 case HIBERNATION_SHUTDOWN: 688 - if (kernel_can_power_off()) 688 + if (kernel_can_power_off()) { 689 + entering_platform_hibernation = true; 689 690 kernel_power_off(); 691 + entering_platform_hibernation = false; 692 + } 690 693 break; 691 694 } 692 695 kernel_halt();