1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * poweroff.c - sysrq handler to gracefully power down machine.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/sysrq.h>
8 #include <linux/init.h>
9 #include <linux/pm.h>
10 #include <linux/workqueue.h>
11 #include <linux/reboot.h>
12 #include <linux/cpumask.h>
13 
14 /*
15  * When the user hits Sys-Rq o to power down the machine this is the
16  * callback we use.
17  */
18 
do_poweroff(struct work_struct * dummy)19 static void do_poweroff(struct work_struct *dummy)
20 {
21 	kernel_power_off();
22 }
23 
24 static DECLARE_WORK(poweroff_work, do_poweroff);
25 
handle_poweroff(int key)26 static void handle_poweroff(int key)
27 {
28 	/* run sysrq poweroff on boot cpu */
29 	schedule_work_on(cpumask_first(cpu_online_mask), &poweroff_work);
30 }
31 
32 static const struct sysrq_key_op	sysrq_poweroff_op = {
33 	.handler        = handle_poweroff,
34 	.help_msg       = "poweroff(o)",
35 	.action_msg     = "Power Off",
36 	.enable_mask	= SYSRQ_ENABLE_BOOT,
37 };
38 
pm_sysrq_init(void)39 static int __init pm_sysrq_init(void)
40 {
41 	register_sysrq_key('o', &sysrq_poweroff_op);
42 	return 0;
43 }
44 
45 subsys_initcall(pm_sysrq_init);
46