1 // SPDX-License-Identifier: GPL-2.0
3 * SCMI Generic SystemPower Control driver.
5 * Copyright (C) 2020-2022 ARM Ltd.
8 * In order to handle platform originated SCMI SystemPower requests (like
9 * shutdowns or cold/warm resets) we register an SCMI Notification notifier
10 * block to react when such SCMI SystemPower events are emitted by platform.
12 * Once such a notification is received we act accordingly to perform the
13 * required system transition depending on the kind of request.
15 * Graceful requests are routed to userspace through the same API methods
16 * (orderly_poweroff/reboot()) used by ACPI when handling ACPI Shutdown bus
19 * Direct forceful requests are not supported since are not meant to be sent
20 * by the SCMI platform to an OSPM like Linux.
22 * Additionally, graceful request notifications can carry an optional timeout
23 * field stating the maximum amount of time allowed by the platform for
24 * completion after which they are converted to forceful ones: the assumption
25 * here is that even graceful requests can be upper-bound by a maximum final
26 * timeout strictly enforced by the platform itself which can ultimately cut
27 * the power off at will anytime; in order to avoid such extreme scenario, we
28 * track progress of graceful requests through the means of a reboot notifier
29 * converting timed-out graceful requests to forceful ones, so at least we
30 * try to perform a clean sync and shutdown/restart before the power is cut.
32 * Given the peculiar nature of SCMI SystemPower protocol, that is being in
33 * charge of triggering system wide shutdown/reboot events, there should be
34 * only one SCMI platform actively emitting SystemPower events.
35 * For this reason the SCMI core takes care to enforce the creation of one
36 * single unique device associated to the SCMI System Power protocol; no matter
37 * how many SCMI platforms are defined on the system, only one can be designated
38 * to support System Power: as a consequence this driver will never be probed
41 * For similar reasons as soon as the first valid SystemPower is received by
42 * this driver and the shutdown/reboot is started, any further notification
43 * possibly emitted by the platform will be ignored.
46 #include <linux/math.h>
47 #include <linux/module.h>
48 #include <linux/mutex.h>
49 #include <linux/printk.h>
50 #include <linux/reboot.h>
51 #include <linux/scmi_protocol.h>
52 #include <linux/slab.h>
53 #include <linux/time64.h>
54 #include <linux/timer.h>
55 #include <linux/types.h>
56 #include <linux/workqueue.h>
62 enum scmi_syspower_state {
64 SCMI_SYSPOWER_IN_PROGRESS,
65 SCMI_SYSPOWER_REBOOTING
69 * struct scmi_syspower_conf - Common configuration
71 * @dev: A reference device
72 * @state: Current SystemPower state
73 * @state_mtx: @state related mutex
74 * @required_transition: The requested transition as decribed in the received
75 * SCMI SystemPower notification
76 * @userspace_nb: The notifier_block registered against the SCMI SystemPower
77 * notification to start the needed userspace interactions.
78 * @reboot_nb: A notifier_block optionally used to track reboot progress
79 * @forceful_work: A worker used to trigger a forceful transition once a
80 * graceful has timed out.
82 struct scmi_syspower_conf {
84 enum scmi_syspower_state state;
85 /* Protect access to state */
86 struct mutex state_mtx;
87 enum scmi_system_events required_transition;
89 struct notifier_block userspace_nb;
90 struct notifier_block reboot_nb;
92 struct delayed_work forceful_work;
95 #define userspace_nb_to_sconf(x) \
96 container_of(x, struct scmi_syspower_conf, userspace_nb)
98 #define reboot_nb_to_sconf(x) \
99 container_of(x, struct scmi_syspower_conf, reboot_nb)
101 #define dwork_to_sconf(x) \
102 container_of(x, struct scmi_syspower_conf, forceful_work)
105 * scmi_reboot_notifier - A reboot notifier to catch an ongoing successful
107 * @nb: Reference to the related notifier block
108 * @reason: The reason for the ongoing reboot
109 * @__unused: The cmd being executed on a restart request (unused)
111 * When an ongoing system transition is detected, compatible with the one
112 * requested by SCMI, cancel the delayed work.
114 * Return: NOTIFY_OK in any case
116 static int scmi_reboot_notifier(struct notifier_block *nb,
117 unsigned long reason, void *__unused)
119 struct scmi_syspower_conf *sc = reboot_nb_to_sconf(nb);
121 mutex_lock(&sc->state_mtx);
125 if (sc->required_transition == SCMI_SYSTEM_SHUTDOWN)
126 sc->state = SCMI_SYSPOWER_REBOOTING;
129 if (sc->required_transition == SCMI_SYSTEM_COLDRESET ||
130 sc->required_transition == SCMI_SYSTEM_WARMRESET)
131 sc->state = SCMI_SYSPOWER_REBOOTING;
137 if (sc->state == SCMI_SYSPOWER_REBOOTING) {
138 dev_dbg(sc->dev, "Reboot in progress...cancel delayed work.\n");
139 cancel_delayed_work_sync(&sc->forceful_work);
141 mutex_unlock(&sc->state_mtx);
147 * scmi_request_forceful_transition - Request forceful SystemPower transition
148 * @sc: A reference to the configuration data
150 * Initiates the required SystemPower transition without involving userspace:
151 * just trigger the action at the kernel level after issuing an emergency
152 * sync. (if possible at all)
155 scmi_request_forceful_transition(struct scmi_syspower_conf *sc)
157 dev_dbg(sc->dev, "Serving forceful request:%d\n",
158 sc->required_transition);
163 switch (sc->required_transition) {
164 case SCMI_SYSTEM_SHUTDOWN:
167 case SCMI_SYSTEM_COLDRESET:
168 case SCMI_SYSTEM_WARMRESET:
169 kernel_restart(NULL);
176 static void scmi_forceful_work_func(struct work_struct *work)
178 struct scmi_syspower_conf *sc;
179 struct delayed_work *dwork;
181 if (system_state > SYSTEM_RUNNING)
184 dwork = to_delayed_work(work);
185 sc = dwork_to_sconf(dwork);
187 dev_dbg(sc->dev, "Graceful request timed out...forcing !\n");
188 mutex_lock(&sc->state_mtx);
189 /* avoid deadlock by unregistering reboot notifier first */
190 unregister_reboot_notifier(&sc->reboot_nb);
191 if (sc->state == SCMI_SYSPOWER_IN_PROGRESS)
192 scmi_request_forceful_transition(sc);
193 mutex_unlock(&sc->state_mtx);
197 * scmi_request_graceful_transition - Request graceful SystemPower transition
198 * @sc: A reference to the configuration data
199 * @timeout_ms: The desired timeout to wait for the shutdown to complete before
200 * system is forcibly shutdown.
202 * Initiates the required SystemPower transition, requesting userspace
203 * co-operation: it uses the same orderly_ methods used by ACPI Shutdown event
206 * Takes care also to register a reboot notifier and to schedule a delayed work
207 * in order to detect if userspace actions are taking too long and in such a
208 * case to trigger a forceful transition.
210 static void scmi_request_graceful_transition(struct scmi_syspower_conf *sc,
211 unsigned int timeout_ms)
213 unsigned int adj_timeout_ms = 0;
218 sc->reboot_nb.notifier_call = &scmi_reboot_notifier;
219 ret = register_reboot_notifier(&sc->reboot_nb);
221 /* Wait only up to 75% of the advertised timeout */
222 adj_timeout_ms = mult_frac(timeout_ms, 3, 4);
223 INIT_DELAYED_WORK(&sc->forceful_work,
224 scmi_forceful_work_func);
225 schedule_delayed_work(&sc->forceful_work,
226 msecs_to_jiffies(adj_timeout_ms));
228 /* Carry on best effort even without a reboot notifier */
230 "Cannot register reboot notifier !\n");
235 "Serving graceful req:%d (timeout_ms:%u adj_timeout_ms:%u)\n",
236 sc->required_transition, timeout_ms, adj_timeout_ms);
238 switch (sc->required_transition) {
239 case SCMI_SYSTEM_SHUTDOWN:
241 * When triggered early at boot-time the 'orderly' call will
242 * partially fail due to the lack of userspace itself, but
243 * the force=true argument will start anyway a successful
246 orderly_poweroff(true);
248 case SCMI_SYSTEM_COLDRESET:
249 case SCMI_SYSTEM_WARMRESET:
258 * scmi_userspace_notifier - Notifier callback to act on SystemPower
260 * @nb: Reference to the related notifier block
261 * @event: The SystemPower notification event id
262 * @data: The SystemPower event report
264 * This callback is in charge of decoding the received SystemPower report
265 * and act accordingly triggering a graceful or forceful system transition.
267 * Note that once a valid SCMI SystemPower event starts being served, any
268 * other following SystemPower notification received from the same SCMI
269 * instance (handle) will be ignored.
271 * Return: NOTIFY_OK once a valid SystemPower event has been successfully
274 static int scmi_userspace_notifier(struct notifier_block *nb,
275 unsigned long event, void *data)
277 struct scmi_system_power_state_notifier_report *er = data;
278 struct scmi_syspower_conf *sc = userspace_nb_to_sconf(nb);
280 if (er->system_state >= SCMI_SYSTEM_POWERUP) {
281 dev_err(sc->dev, "Ignoring unsupported system_state: 0x%X\n",
286 if (!SCMI_SYSPOWER_IS_REQUEST_GRACEFUL(er->flags)) {
287 dev_err(sc->dev, "Ignoring forceful notification.\n");
292 * Bail out if system is already shutting down or an SCMI SystemPower
293 * requested is already being served.
295 if (system_state > SYSTEM_RUNNING)
297 mutex_lock(&sc->state_mtx);
298 if (sc->state != SCMI_SYSPOWER_IDLE) {
300 "Transition already in progress...ignore.\n");
301 mutex_unlock(&sc->state_mtx);
304 sc->state = SCMI_SYSPOWER_IN_PROGRESS;
305 mutex_unlock(&sc->state_mtx);
307 sc->required_transition = er->system_state;
309 /* Leaving a trace in logs of who triggered the shutdown/reboot. */
310 dev_info(sc->dev, "Serving shutdown/reboot request: %d\n",
311 sc->required_transition);
313 scmi_request_graceful_transition(sc, er->timeout);
318 static int scmi_syspower_probe(struct scmi_device *sdev)
321 struct scmi_syspower_conf *sc;
322 struct scmi_handle *handle = sdev->handle;
327 ret = handle->devm_protocol_acquire(sdev, SCMI_PROTOCOL_SYSTEM);
331 sc = devm_kzalloc(&sdev->dev, sizeof(*sc), GFP_KERNEL);
335 sc->state = SCMI_SYSPOWER_IDLE;
336 mutex_init(&sc->state_mtx);
337 sc->required_transition = SCMI_SYSTEM_MAX;
338 sc->userspace_nb.notifier_call = &scmi_userspace_notifier;
339 sc->dev = &sdev->dev;
341 return handle->notify_ops->devm_event_notifier_register(sdev,
342 SCMI_PROTOCOL_SYSTEM,
343 SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
344 NULL, &sc->userspace_nb);
347 static const struct scmi_device_id scmi_id_table[] = {
348 { SCMI_PROTOCOL_SYSTEM, "syspower" },
351 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
353 static struct scmi_driver scmi_system_power_driver = {
354 .name = "scmi-system-power",
355 .probe = scmi_syspower_probe,
356 .id_table = scmi_id_table,
358 module_scmi_driver(scmi_system_power_driver);
361 MODULE_DESCRIPTION("ARM SCMI SystemPower Control driver");
362 MODULE_LICENSE("GPL");