// SPDX-License-Identifier: GPL-2.0 /* * SCMI Generic SystemPower Control driver. * * Copyright (C) 2020-2022 ARM Ltd. */ /* * In order to handle platform originated SCMI SystemPower requests (like * shutdowns or cold/warm resets) we register an SCMI Notification notifier * block to react when such SCMI SystemPower events are emitted by platform. * * Once such a notification is received we act accordingly to perform the * required system transition depending on the kind of request. * * Graceful requests are routed to userspace through the same API methods * (orderly_poweroff/reboot()) used by ACPI when handling ACPI Shutdown bus * events. * * Direct forceful requests are not supported since are not meant to be sent * by the SCMI platform to an OSPM like Linux. * * Additionally, graceful request notifications can carry an optional timeout * field stating the maximum amount of time allowed by the platform for * completion after which they are converted to forceful ones: the assumption * here is that even graceful requests can be upper-bound by a maximum final * timeout strictly enforced by the platform itself which can ultimately cut * the power off at will anytime; in order to avoid such extreme scenario, we * track progress of graceful requests through the means of a reboot notifier * converting timed-out graceful requests to forceful ones, so at least we * try to perform a clean sync and shutdown/restart before the power is cut. * * Given the peculiar nature of SCMI SystemPower protocol, that is being in * charge of triggering system wide shutdown/reboot events, there should be * only one SCMI platform actively emitting SystemPower events. * For this reason the SCMI core takes care to enforce the creation of one * single unique device associated to the SCMI System Power protocol; no matter * how many SCMI platforms are defined on the system, only one can be designated * to support System Power: as a consequence this driver will never be probed * more than once. * * For similar reasons as soon as the first valid SystemPower is received by * this driver and the shutdown/reboot is started, any further notification * possibly emitted by the platform will be ignored. */ #include <linux/math.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/printk.h> #include <linux/reboot.h> #include <linux/scmi_protocol.h> #include <linux/slab.h> #include <linux/suspend.h> #include <linux/time64.h> #include <linux/timer.h> #include <linux/types.h> #include <linux/workqueue.h> #ifndef MODULE #include <linux/fs.h> #endif enum scmi_syspower_state { … }; /** * struct scmi_syspower_conf - Common configuration * * @dev: A reference device * @state: Current SystemPower state * @state_mtx: @state related mutex * @required_transition: The requested transition as decribed in the received * SCMI SystemPower notification * @userspace_nb: The notifier_block registered against the SCMI SystemPower * notification to start the needed userspace interactions. * @reboot_nb: A notifier_block optionally used to track reboot progress * @forceful_work: A worker used to trigger a forceful transition once a * graceful has timed out. * @suspend_work: A worker used to trigger system suspend */ struct scmi_syspower_conf { … }; #define userspace_nb_to_sconf(x) … #define reboot_nb_to_sconf(x) … #define dwork_to_sconf(x) … /** * scmi_reboot_notifier - A reboot notifier to catch an ongoing successful * system transition * @nb: Reference to the related notifier block * @reason: The reason for the ongoing reboot * @__unused: The cmd being executed on a restart request (unused) * * When an ongoing system transition is detected, compatible with the one * requested by SCMI, cancel the delayed work. * * Return: NOTIFY_OK in any case */ static int scmi_reboot_notifier(struct notifier_block *nb, unsigned long reason, void *__unused) { … } /** * scmi_request_forceful_transition - Request forceful SystemPower transition * @sc: A reference to the configuration data * * Initiates the required SystemPower transition without involving userspace: * just trigger the action at the kernel level after issuing an emergency * sync. (if possible at all) */ static inline void scmi_request_forceful_transition(struct scmi_syspower_conf *sc) { … } static void scmi_forceful_work_func(struct work_struct *work) { … } /** * scmi_request_graceful_transition - Request graceful SystemPower transition * @sc: A reference to the configuration data * @timeout_ms: The desired timeout to wait for the shutdown to complete before * system is forcibly shutdown. * * Initiates the required SystemPower transition, requesting userspace * co-operation: it uses the same orderly_ methods used by ACPI Shutdown event * processing. * * Takes care also to register a reboot notifier and to schedule a delayed work * in order to detect if userspace actions are taking too long and in such a * case to trigger a forceful transition. */ static void scmi_request_graceful_transition(struct scmi_syspower_conf *sc, unsigned int timeout_ms) { … } /** * scmi_userspace_notifier - Notifier callback to act on SystemPower * Notifications * @nb: Reference to the related notifier block * @event: The SystemPower notification event id * @data: The SystemPower event report * * This callback is in charge of decoding the received SystemPower report * and act accordingly triggering a graceful or forceful system transition. * * Note that once a valid SCMI SystemPower event starts being served, any * other following SystemPower notification received from the same SCMI * instance (handle) will be ignored. * * Return: NOTIFY_OK once a valid SystemPower event has been successfully * processed. */ static int scmi_userspace_notifier(struct notifier_block *nb, unsigned long event, void *data) { … } static void scmi_suspend_work_func(struct work_struct *work) { … } static int scmi_syspower_probe(struct scmi_device *sdev) { … } static const struct scmi_device_id scmi_id_table[] = …; MODULE_DEVICE_TABLE(scmi, scmi_id_table); static struct scmi_driver scmi_system_power_driver = …; module_scmi_driver(…) …; MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;