blob: 2a3717fc24eaee6b1811515d14d260e5b18b6b81 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joel Stanley7f43e712015-01-30 17:13:08 +10302/*
3 * PowerNV OPAL power control for graceful shutdown handling
4 *
5 * Copyright 2015 IBM Corp.
Joel Stanley7f43e712015-01-30 17:13:08 +10306 */
7
Vipin K Parashar3b476aad2015-07-08 16:36:01 +05308#define pr_fmt(fmt) "opal-power: " fmt
9
Joel Stanley7f43e712015-01-30 17:13:08 +103010#include <linux/kernel.h>
11#include <linux/reboot.h>
12#include <linux/notifier.h>
Vipin K Parashar3b476aad2015-07-08 16:36:01 +053013#include <linux/of.h>
Joel Stanley7f43e712015-01-30 17:13:08 +103014
15#include <asm/opal.h>
16#include <asm/machdep.h>
17
18#define SOFT_OFF 0x00
19#define SOFT_REBOOT 0x01
20
Vipin K Parashar3b476aad2015-07-08 16:36:01 +053021/* Detect EPOW event */
22static bool detect_epow(void)
Joel Stanley7f43e712015-01-30 17:13:08 +103023{
Vipin K Parashar3b476aad2015-07-08 16:36:01 +053024 u16 epow;
25 int i, rc;
26 __be16 epow_classes;
27 __be16 opal_epow_status[OPAL_SYSEPOW_MAX] = {0};
28
29 /*
30 * Check for EPOW event. Kernel sends supported EPOW classes info
31 * to OPAL. OPAL returns EPOW info along with classes present.
32 */
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");
37 return false;
38 }
39
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]);
43
44 /* Filter events which do not need shutdown. */
45 if (i == OPAL_SYSEPOW_POWER)
46 epow &= ~(OPAL_SYSPOWER_CHNG | OPAL_SYSPOWER_FAIL |
47 OPAL_SYSPOWER_INCL);
48 if (epow)
49 return true;
50 }
51
52 return false;
53}
54
55/* Check for existing EPOW, DPO events */
56static bool poweroff_pending(void)
57{
58 int rc;
59 __be64 opal_dpo_timeout;
60
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");
65 return true;
66 }
67
68 /* Check for EPOW event */
69 if (detect_epow()) {
70 pr_info("Existing EPOW event detected.\n");
71 return true;
72 }
73
74 return false;
75}
76
77/* OPAL power-control events notifier */
78static int opal_power_control_event(struct notifier_block *nb,
79 unsigned long msg_type, void *msg)
80{
Joel Stanley7f43e712015-01-30 17:13:08 +103081 uint64_t type;
82
Vipin K Parashar3b476aad2015-07-08 16:36:01 +053083 switch (msg_type) {
84 case OPAL_MSG_EPOW:
85 if (detect_epow()) {
86 pr_info("EPOW msg received. Powering off system\n");
87 orderly_poweroff(true);
88 }
Joel Stanleye2433042015-04-15 16:16:56 -070089 break;
Vipin K Parashar3b476aad2015-07-08 16:36:01 +053090 case OPAL_MSG_DPO:
91 pr_info("DPO msg received. Powering off system\n");
Joel Stanley7f43e712015-01-30 17:13:08 +103092 orderly_poweroff(true);
93 break;
Vipin K Parashar3b476aad2015-07-08 16:36:01 +053094 case OPAL_MSG_SHUTDOWN:
95 type = be64_to_cpu(((struct opal_msg *)msg)->params[0]);
96 switch (type) {
97 case SOFT_REBOOT:
98 pr_info("Reboot requested\n");
99 orderly_reboot();
100 break;
101 case SOFT_OFF:
102 pr_info("Poweroff requested\n");
103 orderly_poweroff(true);
104 break;
105 default:
106 pr_err("Unknown power-control type %llu\n", type);
107 }
108 break;
Joel Stanley7f43e712015-01-30 17:13:08 +1030109 default:
Vipin K Parashar3b476aad2015-07-08 16:36:01 +0530110 pr_err("Unknown OPAL message type %lu\n", msg_type);
Joel Stanley7f43e712015-01-30 17:13:08 +1030111 }
112
113 return 0;
114}
115
Vipin K Parashar3b476aad2015-07-08 16:36:01 +0530116/* OPAL EPOW event notifier block */
117static struct notifier_block opal_epow_nb = {
118 .notifier_call = opal_power_control_event,
119 .next = NULL,
120 .priority = 0,
121};
122
123/* OPAL DPO event notifier block */
124static struct notifier_block opal_dpo_nb = {
125 .notifier_call = opal_power_control_event,
126 .next = NULL,
127 .priority = 0,
128};
129
130/* OPAL power-control event notifier block */
Joel Stanley7f43e712015-01-30 17:13:08 +1030131static struct notifier_block opal_power_control_nb = {
132 .notifier_call = opal_power_control_event,
133 .next = NULL,
134 .priority = 0,
135};
136
Mahesh Salgaonkar08fb7262018-12-13 11:17:31 +0530137int __init opal_power_control_init(void)
Joel Stanley7f43e712015-01-30 17:13:08 +1030138{
Vipin K Parashar3b476aad2015-07-08 16:36:01 +0530139 int ret, supported = 0;
140 struct device_node *np;
Joel Stanley7f43e712015-01-30 17:13:08 +1030141
Vipin K Parashar3b476aad2015-07-08 16:36:01 +0530142 /* Register OPAL power-control events notifier */
Joel Stanley7f43e712015-01-30 17:13:08 +1030143 ret = opal_message_notifier_register(OPAL_MSG_SHUTDOWN,
Vipin K Parashar3b476aad2015-07-08 16:36:01 +0530144 &opal_power_control_nb);
145 if (ret)
146 pr_err("Failed to register SHUTDOWN notifier, ret = %d\n", ret);
147
148 /* Determine OPAL EPOW, DPO support */
149 np = of_find_node_by_path("/ibm,opal/epow");
150 if (np) {
151 supported = of_device_is_compatible(np, "ibm,opal-v3-epow");
152 of_node_put(np);
Joel Stanley7f43e712015-01-30 17:13:08 +1030153 }
154
Vipin K Parashar3b476aad2015-07-08 16:36:01 +0530155 if (!supported)
156 return 0;
157 pr_info("OPAL EPOW, DPO support detected.\n");
158
159 /* Register EPOW event notifier */
160 ret = opal_message_notifier_register(OPAL_MSG_EPOW, &opal_epow_nb);
161 if (ret)
162 pr_err("Failed to register EPOW notifier, ret = %d\n", ret);
163
164 /* Register DPO event notifier */
165 ret = opal_message_notifier_register(OPAL_MSG_DPO, &opal_dpo_nb);
166 if (ret)
167 pr_err("Failed to register DPO notifier, ret = %d\n", ret);
168
169 /* Check for any pending EPOW or DPO events. */
170 if (poweroff_pending())
171 orderly_poweroff(true);
172
Joel Stanley7f43e712015-01-30 17:13:08 +1030173 return 0;
174}