1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PowerNV OPAL power control for graceful shutdown handling
5 * Copyright 2015 IBM Corp.
8 #define pr_fmt(fmt) "opal-power: " fmt
10 #include <linux/kernel.h>
11 #include <linux/reboot.h>
12 #include <linux/notifier.h>
16 #include <asm/machdep.h>
19 #define SOFT_REBOOT 0x01
21 /* Detect EPOW event */
22 static bool detect_epow(void)
27 __be16 opal_epow_status
[OPAL_SYSEPOW_MAX
] = {0};
30 * Check for EPOW event. Kernel sends supported EPOW classes info
31 * to OPAL. OPAL returns EPOW info along with classes present.
33 epow_classes
= cpu_to_be16(OPAL_SYSEPOW_MAX
);
34 rc
= opal_get_epow_status(opal_epow_status
, &epow_classes
);
35 if (rc
!= OPAL_SUCCESS
) {
36 pr_err("Failed to get EPOW event information\n");
40 /* Look for EPOW events present */
41 for (i
= 0; i
< be16_to_cpu(epow_classes
); i
++) {
42 epow
= be16_to_cpu(opal_epow_status
[i
]);
44 /* Filter events which do not need shutdown. */
45 if (i
== OPAL_SYSEPOW_POWER
)
46 epow
&= ~(OPAL_SYSPOWER_CHNG
| OPAL_SYSPOWER_FAIL
|
55 /* Check for existing EPOW, DPO events */
56 static bool poweroff_pending(void)
59 __be64 opal_dpo_timeout
;
61 /* Check for DPO event */
62 rc
= opal_get_dpo_status(&opal_dpo_timeout
);
63 if (rc
== OPAL_SUCCESS
) {
64 pr_info("Existing DPO event detected.\n");
68 /* Check for EPOW event */
70 pr_info("Existing EPOW event detected.\n");
77 /* OPAL power-control events notifier */
78 static int opal_power_control_event(struct notifier_block
*nb
,
79 unsigned long msg_type
, void *msg
)
86 pr_info("EPOW msg received. Powering off system\n");
87 orderly_poweroff(true);
91 pr_info("DPO msg received. Powering off system\n");
92 orderly_poweroff(true);
94 case OPAL_MSG_SHUTDOWN
:
95 type
= be64_to_cpu(((struct opal_msg
*)msg
)->params
[0]);
98 pr_info("Reboot requested\n");
102 pr_info("Poweroff requested\n");
103 orderly_poweroff(true);
106 pr_err("Unknown power-control type %llu\n", type
);
110 pr_err("Unknown OPAL message type %lu\n", msg_type
);
116 /* OPAL EPOW event notifier block */
117 static struct notifier_block opal_epow_nb
= {
118 .notifier_call
= opal_power_control_event
,
123 /* OPAL DPO event notifier block */
124 static struct notifier_block opal_dpo_nb
= {
125 .notifier_call
= opal_power_control_event
,
130 /* OPAL power-control event notifier block */
131 static struct notifier_block opal_power_control_nb
= {
132 .notifier_call
= opal_power_control_event
,
137 int __init
opal_power_control_init(void)
139 int ret
, supported
= 0;
140 struct device_node
*np
;
142 /* Register OPAL power-control events notifier */
143 ret
= opal_message_notifier_register(OPAL_MSG_SHUTDOWN
,
144 &opal_power_control_nb
);
146 pr_err("Failed to register SHUTDOWN notifier, ret = %d\n", ret
);
148 /* Determine OPAL EPOW, DPO support */
149 np
= of_find_node_by_path("/ibm,opal/epow");
151 supported
= of_device_is_compatible(np
, "ibm,opal-v3-epow");
157 pr_info("OPAL EPOW, DPO support detected.\n");
159 /* Register EPOW event notifier */
160 ret
= opal_message_notifier_register(OPAL_MSG_EPOW
, &opal_epow_nb
);
162 pr_err("Failed to register EPOW notifier, ret = %d\n", ret
);
164 /* Register DPO event notifier */
165 ret
= opal_message_notifier_register(OPAL_MSG_DPO
, &opal_dpo_nb
);
167 pr_err("Failed to register DPO notifier, ret = %d\n", ret
);
169 /* Check for any pending EPOW or DPO events. */
170 if (poweroff_pending())
171 orderly_poweroff(true);