xref: /openbmc/linux/drivers/firmware/efi/reboot.c (revision 09c434b8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014 Intel Corporation; author Matt Fleming
4  * Copyright (c) 2014 Red Hat, Inc., Mark Salter <msalter@redhat.com>
5  */
6 #include <linux/efi.h>
7 #include <linux/reboot.h>
8 
9 static void (*orig_pm_power_off)(void);
10 
11 int efi_reboot_quirk_mode = -1;
12 
13 void efi_reboot(enum reboot_mode reboot_mode, const char *__unused)
14 {
15 	const char *str[] = { "cold", "warm", "shutdown", "platform" };
16 	int efi_mode, cap_reset_mode;
17 
18 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
19 		return;
20 
21 	switch (reboot_mode) {
22 	case REBOOT_WARM:
23 	case REBOOT_SOFT:
24 		efi_mode = EFI_RESET_WARM;
25 		break;
26 	default:
27 		efi_mode = EFI_RESET_COLD;
28 		break;
29 	}
30 
31 	/*
32 	 * If a quirk forced an EFI reset mode, always use that.
33 	 */
34 	if (efi_reboot_quirk_mode != -1)
35 		efi_mode = efi_reboot_quirk_mode;
36 
37 	if (efi_capsule_pending(&cap_reset_mode)) {
38 		if (efi_mode != cap_reset_mode)
39 			printk(KERN_CRIT "efi: %s reset requested but pending "
40 			       "capsule update requires %s reset... Performing "
41 			       "%s reset.\n", str[efi_mode], str[cap_reset_mode],
42 			       str[cap_reset_mode]);
43 		efi_mode = cap_reset_mode;
44 	}
45 
46 	efi.reset_system(efi_mode, EFI_SUCCESS, 0, NULL);
47 }
48 
49 bool __weak efi_poweroff_required(void)
50 {
51 	return false;
52 }
53 
54 static void efi_power_off(void)
55 {
56 	efi.reset_system(EFI_RESET_SHUTDOWN, EFI_SUCCESS, 0, NULL);
57 	/*
58 	 * The above call should not return, if it does fall back to
59 	 * the original power off method (typically ACPI poweroff).
60 	 */
61 	if (orig_pm_power_off)
62 		orig_pm_power_off();
63 }
64 
65 static int __init efi_shutdown_init(void)
66 {
67 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
68 		return -ENODEV;
69 
70 	if (efi_poweroff_required()) {
71 		orig_pm_power_off = pm_power_off;
72 		pm_power_off = efi_power_off;
73 	}
74 
75 	return 0;
76 }
77 late_initcall(efi_shutdown_init);
78