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