xref: /openbmc/linux/kernel/reboot.c (revision 232edc2f72f5beda3586360de6254d443820050a)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
215d94b82SRobin Holt /*
315d94b82SRobin Holt  *  linux/kernel/reboot.c
415d94b82SRobin Holt  *
515d94b82SRobin Holt  *  Copyright (C) 2013  Linus Torvalds
615d94b82SRobin Holt  */
715d94b82SRobin Holt 
8972ee83dSRobin Holt #define pr_fmt(fmt)	"reboot: " fmt
9972ee83dSRobin Holt 
10dfa19b11SMatti Vaittinen #include <linux/atomic.h>
111b3a5d02SRobin Holt #include <linux/ctype.h>
1215d94b82SRobin Holt #include <linux/export.h>
1315d94b82SRobin Holt #include <linux/kexec.h>
1415d94b82SRobin Holt #include <linux/kmod.h>
1515d94b82SRobin Holt #include <linux/kmsg_dump.h>
1615d94b82SRobin Holt #include <linux/reboot.h>
1715d94b82SRobin Holt #include <linux/suspend.h>
1815d94b82SRobin Holt #include <linux/syscalls.h>
1915d94b82SRobin Holt #include <linux/syscore_ops.h>
2015d94b82SRobin Holt #include <linux/uaccess.h>
2115d94b82SRobin Holt 
2215d94b82SRobin Holt /*
2315d94b82SRobin Holt  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
2415d94b82SRobin Holt  */
2515d94b82SRobin Holt 
2615d94b82SRobin Holt int C_A_D = 1;
2715d94b82SRobin Holt struct pid *cad_pid;
2815d94b82SRobin Holt EXPORT_SYMBOL(cad_pid);
2915d94b82SRobin Holt 
30fb37409aSMike Rapoport #if defined(CONFIG_ARM)
311b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE		= REBOOT_HARD
321b3a5d02SRobin Holt #else
331b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE
341b3a5d02SRobin Holt #endif
351b3a5d02SRobin Holt enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
36d46b3f5bSShawn Guo EXPORT_SYMBOL_GPL(reboot_mode);
37b287a25aSAaro Koskinen enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
381b3a5d02SRobin Holt 
39e2f0b88eSChuansheng Liu /*
40e2f0b88eSChuansheng Liu  * This variable is used privately to keep track of whether or not
41e2f0b88eSChuansheng Liu  * reboot_type is still set to its default value (i.e., reboot= hasn't
42e2f0b88eSChuansheng Liu  * been set on the command line).  This is needed so that we can
43e2f0b88eSChuansheng Liu  * suppress DMI scanning for reboot quirks.  Without it, it's
44e2f0b88eSChuansheng Liu  * impossible to override a faulty reboot quirk without recompiling.
45e2f0b88eSChuansheng Liu  */
46e2f0b88eSChuansheng Liu int reboot_default = 1;
471b3a5d02SRobin Holt int reboot_cpu;
481b3a5d02SRobin Holt enum reboot_type reboot_type = BOOT_ACPI;
491b3a5d02SRobin Holt int reboot_force;
501b3a5d02SRobin Holt 
51*232edc2fSDmitry Osipenko struct sys_off_handler {
52*232edc2fSDmitry Osipenko 	struct notifier_block nb;
53*232edc2fSDmitry Osipenko 	int (*sys_off_cb)(struct sys_off_data *data);
54*232edc2fSDmitry Osipenko 	void *cb_data;
55*232edc2fSDmitry Osipenko 	enum sys_off_mode mode;
56*232edc2fSDmitry Osipenko 	bool blocking;
57*232edc2fSDmitry Osipenko 	void *list;
58*232edc2fSDmitry Osipenko };
59*232edc2fSDmitry Osipenko 
6015d94b82SRobin Holt /*
6115d94b82SRobin Holt  * If set, this is used for preparing the system to power off.
6215d94b82SRobin Holt  */
6315d94b82SRobin Holt 
6415d94b82SRobin Holt void (*pm_power_off_prepare)(void);
6574f008f2SOleksij Rempel EXPORT_SYMBOL_GPL(pm_power_off_prepare);
6615d94b82SRobin Holt 
6715d94b82SRobin Holt /**
6815d94b82SRobin Holt  *	emergency_restart - reboot the system
6915d94b82SRobin Holt  *
7015d94b82SRobin Holt  *	Without shutting down any hardware or taking any locks
7115d94b82SRobin Holt  *	reboot the system.  This is called when we know we are in
7215d94b82SRobin Holt  *	trouble so this is our best effort to reboot.  This is
7315d94b82SRobin Holt  *	safe to call in interrupt context.
7415d94b82SRobin Holt  */
7515d94b82SRobin Holt void emergency_restart(void)
7615d94b82SRobin Holt {
7715d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_EMERG);
7815d94b82SRobin Holt 	machine_emergency_restart();
7915d94b82SRobin Holt }
8015d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart);
8115d94b82SRobin Holt 
8215d94b82SRobin Holt void kernel_restart_prepare(char *cmd)
8315d94b82SRobin Holt {
8415d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
8515d94b82SRobin Holt 	system_state = SYSTEM_RESTART;
8615d94b82SRobin Holt 	usermodehelper_disable();
8715d94b82SRobin Holt 	device_shutdown();
8815d94b82SRobin Holt }
8915d94b82SRobin Holt 
9015d94b82SRobin Holt /**
9115d94b82SRobin Holt  *	register_reboot_notifier - Register function to be called at reboot time
9215d94b82SRobin Holt  *	@nb: Info about notifier function to be called
9315d94b82SRobin Holt  *
9415d94b82SRobin Holt  *	Registers a function with the list of functions
9515d94b82SRobin Holt  *	to be called at reboot time.
9615d94b82SRobin Holt  *
9715d94b82SRobin Holt  *	Currently always returns zero, as blocking_notifier_chain_register()
9815d94b82SRobin Holt  *	always returns zero.
9915d94b82SRobin Holt  */
10015d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb)
10115d94b82SRobin Holt {
10215d94b82SRobin Holt 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
10315d94b82SRobin Holt }
10415d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier);
10515d94b82SRobin Holt 
10615d94b82SRobin Holt /**
10715d94b82SRobin Holt  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
10815d94b82SRobin Holt  *	@nb: Hook to be unregistered
10915d94b82SRobin Holt  *
11015d94b82SRobin Holt  *	Unregisters a previously registered reboot
11115d94b82SRobin Holt  *	notifier function.
11215d94b82SRobin Holt  *
11315d94b82SRobin Holt  *	Returns zero on success, or %-ENOENT on failure.
11415d94b82SRobin Holt  */
11515d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb)
11615d94b82SRobin Holt {
11715d94b82SRobin Holt 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
11815d94b82SRobin Holt }
11915d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier);
12015d94b82SRobin Holt 
1212d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res)
1222d8364baSAndrey Smirnov {
1232d8364baSAndrey Smirnov 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
1242d8364baSAndrey Smirnov }
1252d8364baSAndrey Smirnov 
1262d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
1272d8364baSAndrey Smirnov {
1282d8364baSAndrey Smirnov 	struct notifier_block **rcnb;
1292d8364baSAndrey Smirnov 	int ret;
1302d8364baSAndrey Smirnov 
1312d8364baSAndrey Smirnov 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
1322d8364baSAndrey Smirnov 			    sizeof(*rcnb), GFP_KERNEL);
1332d8364baSAndrey Smirnov 	if (!rcnb)
1342d8364baSAndrey Smirnov 		return -ENOMEM;
1352d8364baSAndrey Smirnov 
1362d8364baSAndrey Smirnov 	ret = register_reboot_notifier(nb);
1372d8364baSAndrey Smirnov 	if (!ret) {
1382d8364baSAndrey Smirnov 		*rcnb = nb;
1392d8364baSAndrey Smirnov 		devres_add(dev, rcnb);
1402d8364baSAndrey Smirnov 	} else {
1412d8364baSAndrey Smirnov 		devres_free(rcnb);
1422d8364baSAndrey Smirnov 	}
1432d8364baSAndrey Smirnov 
1442d8364baSAndrey Smirnov 	return ret;
1452d8364baSAndrey Smirnov }
1462d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier);
1472d8364baSAndrey Smirnov 
148b63adb97SGuenter Roeck /*
149b63adb97SGuenter Roeck  *	Notifier list for kernel code which wants to be called
150b63adb97SGuenter Roeck  *	to restart the system.
151b63adb97SGuenter Roeck  */
152b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
153b63adb97SGuenter Roeck 
154b63adb97SGuenter Roeck /**
155b63adb97SGuenter Roeck  *	register_restart_handler - Register function to be called to reset
156b63adb97SGuenter Roeck  *				   the system
157b63adb97SGuenter Roeck  *	@nb: Info about handler function to be called
158b63adb97SGuenter Roeck  *	@nb->priority:	Handler priority. Handlers should follow the
159b63adb97SGuenter Roeck  *			following guidelines for setting priorities.
160b63adb97SGuenter Roeck  *			0:	Restart handler of last resort,
161b63adb97SGuenter Roeck  *				with limited restart capabilities
162b63adb97SGuenter Roeck  *			128:	Default restart handler; use if no other
163b63adb97SGuenter Roeck  *				restart handler is expected to be available,
164b63adb97SGuenter Roeck  *				and/or if restart functionality is
165b63adb97SGuenter Roeck  *				sufficient to restart the entire system
166b63adb97SGuenter Roeck  *			255:	Highest priority restart handler, will
167b63adb97SGuenter Roeck  *				preempt all other restart handlers
168b63adb97SGuenter Roeck  *
169b63adb97SGuenter Roeck  *	Registers a function with code to be called to restart the
170b63adb97SGuenter Roeck  *	system.
171b63adb97SGuenter Roeck  *
172b63adb97SGuenter Roeck  *	Registered functions will be called from machine_restart as last
173b63adb97SGuenter Roeck  *	step of the restart sequence (if the architecture specific
174b63adb97SGuenter Roeck  *	machine_restart function calls do_kernel_restart - see below
175b63adb97SGuenter Roeck  *	for details).
176b63adb97SGuenter Roeck  *	Registered functions are expected to restart the system immediately.
177b63adb97SGuenter Roeck  *	If more than one function is registered, the restart handler priority
178b63adb97SGuenter Roeck  *	selects which function will be called first.
179b63adb97SGuenter Roeck  *
180b63adb97SGuenter Roeck  *	Restart handlers are expected to be registered from non-architecture
181b63adb97SGuenter Roeck  *	code, typically from drivers. A typical use case would be a system
182b63adb97SGuenter Roeck  *	where restart functionality is provided through a watchdog. Multiple
183b63adb97SGuenter Roeck  *	restart handlers may exist; for example, one restart handler might
184b63adb97SGuenter Roeck  *	restart the entire system, while another only restarts the CPU.
185b63adb97SGuenter Roeck  *	In such cases, the restart handler which only restarts part of the
186b63adb97SGuenter Roeck  *	hardware is expected to register with low priority to ensure that
187b63adb97SGuenter Roeck  *	it only runs if no other means to restart the system is available.
188b63adb97SGuenter Roeck  *
189b63adb97SGuenter Roeck  *	Currently always returns zero, as atomic_notifier_chain_register()
190b63adb97SGuenter Roeck  *	always returns zero.
191b63adb97SGuenter Roeck  */
192b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb)
193b63adb97SGuenter Roeck {
194b63adb97SGuenter Roeck 	return atomic_notifier_chain_register(&restart_handler_list, nb);
195b63adb97SGuenter Roeck }
196b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler);
197b63adb97SGuenter Roeck 
198b63adb97SGuenter Roeck /**
199b63adb97SGuenter Roeck  *	unregister_restart_handler - Unregister previously registered
200b63adb97SGuenter Roeck  *				     restart handler
201b63adb97SGuenter Roeck  *	@nb: Hook to be unregistered
202b63adb97SGuenter Roeck  *
203b63adb97SGuenter Roeck  *	Unregisters a previously registered restart handler function.
204b63adb97SGuenter Roeck  *
205b63adb97SGuenter Roeck  *	Returns zero on success, or %-ENOENT on failure.
206b63adb97SGuenter Roeck  */
207b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb)
208b63adb97SGuenter Roeck {
209b63adb97SGuenter Roeck 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
210b63adb97SGuenter Roeck }
211b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler);
212b63adb97SGuenter Roeck 
213b63adb97SGuenter Roeck /**
214b63adb97SGuenter Roeck  *	do_kernel_restart - Execute kernel restart handler call chain
215b63adb97SGuenter Roeck  *
216b63adb97SGuenter Roeck  *	Calls functions registered with register_restart_handler.
217b63adb97SGuenter Roeck  *
218b63adb97SGuenter Roeck  *	Expected to be called from machine_restart as last step of the restart
219b63adb97SGuenter Roeck  *	sequence.
220b63adb97SGuenter Roeck  *
221b63adb97SGuenter Roeck  *	Restarts the system immediately if a restart handler function has been
222b63adb97SGuenter Roeck  *	registered. Otherwise does nothing.
223b63adb97SGuenter Roeck  */
224b63adb97SGuenter Roeck void do_kernel_restart(char *cmd)
225b63adb97SGuenter Roeck {
226b63adb97SGuenter Roeck 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
227b63adb97SGuenter Roeck }
228b63adb97SGuenter Roeck 
229c97102baSVivek Goyal void migrate_to_reboot_cpu(void)
23015d94b82SRobin Holt {
23115d94b82SRobin Holt 	/* The boot cpu is always logical cpu 0 */
2321b3a5d02SRobin Holt 	int cpu = reboot_cpu;
23315d94b82SRobin Holt 
23415d94b82SRobin Holt 	cpu_hotplug_disable();
23515d94b82SRobin Holt 
23615d94b82SRobin Holt 	/* Make certain the cpu I'm about to reboot on is online */
23715d94b82SRobin Holt 	if (!cpu_online(cpu))
23815d94b82SRobin Holt 		cpu = cpumask_first(cpu_online_mask);
23915d94b82SRobin Holt 
24015d94b82SRobin Holt 	/* Prevent races with other tasks migrating this task */
24115d94b82SRobin Holt 	current->flags |= PF_NO_SETAFFINITY;
24215d94b82SRobin Holt 
24315d94b82SRobin Holt 	/* Make certain I only run on the appropriate processor */
24415d94b82SRobin Holt 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
24515d94b82SRobin Holt }
24615d94b82SRobin Holt 
24715d94b82SRobin Holt /**
24815d94b82SRobin Holt  *	kernel_restart - reboot the system
24915d94b82SRobin Holt  *	@cmd: pointer to buffer containing command to execute for restart
25015d94b82SRobin Holt  *		or %NULL
25115d94b82SRobin Holt  *
25215d94b82SRobin Holt  *	Shutdown everything and perform a clean reboot.
25315d94b82SRobin Holt  *	This is not safe to call in interrupt context.
25415d94b82SRobin Holt  */
25515d94b82SRobin Holt void kernel_restart(char *cmd)
25615d94b82SRobin Holt {
25715d94b82SRobin Holt 	kernel_restart_prepare(cmd);
25815d94b82SRobin Holt 	migrate_to_reboot_cpu();
25915d94b82SRobin Holt 	syscore_shutdown();
26015d94b82SRobin Holt 	if (!cmd)
261972ee83dSRobin Holt 		pr_emerg("Restarting system\n");
26215d94b82SRobin Holt 	else
263972ee83dSRobin Holt 		pr_emerg("Restarting system with command '%s'\n", cmd);
2646d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
26515d94b82SRobin Holt 	machine_restart(cmd);
26615d94b82SRobin Holt }
26715d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart);
26815d94b82SRobin Holt 
26915d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state)
27015d94b82SRobin Holt {
27115d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list,
27215d94b82SRobin Holt 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
27315d94b82SRobin Holt 	system_state = state;
27415d94b82SRobin Holt 	usermodehelper_disable();
27515d94b82SRobin Holt 	device_shutdown();
27615d94b82SRobin Holt }
27715d94b82SRobin Holt /**
27815d94b82SRobin Holt  *	kernel_halt - halt the system
27915d94b82SRobin Holt  *
28015d94b82SRobin Holt  *	Shutdown everything and perform a clean system halt.
28115d94b82SRobin Holt  */
28215d94b82SRobin Holt void kernel_halt(void)
28315d94b82SRobin Holt {
28415d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_HALT);
28515d94b82SRobin Holt 	migrate_to_reboot_cpu();
28615d94b82SRobin Holt 	syscore_shutdown();
287972ee83dSRobin Holt 	pr_emerg("System halted\n");
2886d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
28915d94b82SRobin Holt 	machine_halt();
29015d94b82SRobin Holt }
29115d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt);
29215d94b82SRobin Holt 
293*232edc2fSDmitry Osipenko /*
294*232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
295*232edc2fSDmitry Osipenko  *	to prepare system for power off.
296*232edc2fSDmitry Osipenko  */
297*232edc2fSDmitry Osipenko static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
298*232edc2fSDmitry Osipenko 
299*232edc2fSDmitry Osipenko /*
300*232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
301*232edc2fSDmitry Osipenko  *	to power off system.
302*232edc2fSDmitry Osipenko  */
303*232edc2fSDmitry Osipenko static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
304*232edc2fSDmitry Osipenko 
305*232edc2fSDmitry Osipenko static int sys_off_notify(struct notifier_block *nb,
306*232edc2fSDmitry Osipenko 			  unsigned long mode, void *cmd)
307*232edc2fSDmitry Osipenko {
308*232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
309*232edc2fSDmitry Osipenko 	struct sys_off_data data = {};
310*232edc2fSDmitry Osipenko 
311*232edc2fSDmitry Osipenko 	handler = container_of(nb, struct sys_off_handler, nb);
312*232edc2fSDmitry Osipenko 	data.cb_data = handler->cb_data;
313*232edc2fSDmitry Osipenko 	data.mode = mode;
314*232edc2fSDmitry Osipenko 	data.cmd = cmd;
315*232edc2fSDmitry Osipenko 
316*232edc2fSDmitry Osipenko 	return handler->sys_off_cb(&data);
317*232edc2fSDmitry Osipenko }
318*232edc2fSDmitry Osipenko 
319*232edc2fSDmitry Osipenko /**
320*232edc2fSDmitry Osipenko  *	register_sys_off_handler - Register sys-off handler
321*232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
322*232edc2fSDmitry Osipenko  *	@priority: Handler priority
323*232edc2fSDmitry Osipenko  *	@callback: Callback function
324*232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
325*232edc2fSDmitry Osipenko  *
326*232edc2fSDmitry Osipenko  *	Registers system power-off or restart handler that will be invoked
327*232edc2fSDmitry Osipenko  *	at the step corresponding to the given sys-off mode. Handler's callback
328*232edc2fSDmitry Osipenko  *	should return NOTIFY_DONE to permit execution of the next handler in
329*232edc2fSDmitry Osipenko  *	the call chain or NOTIFY_STOP to break the chain (in error case for
330*232edc2fSDmitry Osipenko  *	example).
331*232edc2fSDmitry Osipenko  *
332*232edc2fSDmitry Osipenko  *	Multiple handlers can be registered at the default priority level.
333*232edc2fSDmitry Osipenko  *
334*232edc2fSDmitry Osipenko  *	Only one handler can be registered at the non-default priority level,
335*232edc2fSDmitry Osipenko  *	otherwise ERR_PTR(-EBUSY) is returned.
336*232edc2fSDmitry Osipenko  *
337*232edc2fSDmitry Osipenko  *	Returns a new instance of struct sys_off_handler on success, or
338*232edc2fSDmitry Osipenko  *	an ERR_PTR()-encoded error code otherwise.
339*232edc2fSDmitry Osipenko  */
340*232edc2fSDmitry Osipenko struct sys_off_handler *
341*232edc2fSDmitry Osipenko register_sys_off_handler(enum sys_off_mode mode,
342*232edc2fSDmitry Osipenko 			 int priority,
343*232edc2fSDmitry Osipenko 			 int (*callback)(struct sys_off_data *data),
344*232edc2fSDmitry Osipenko 			 void *cb_data)
345*232edc2fSDmitry Osipenko {
346*232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
347*232edc2fSDmitry Osipenko 	int err;
348*232edc2fSDmitry Osipenko 
349*232edc2fSDmitry Osipenko 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
350*232edc2fSDmitry Osipenko 	if (!handler)
351*232edc2fSDmitry Osipenko 		return ERR_PTR(-ENOMEM);
352*232edc2fSDmitry Osipenko 
353*232edc2fSDmitry Osipenko 	switch (mode) {
354*232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF_PREPARE:
355*232edc2fSDmitry Osipenko 		handler->list = &power_off_prep_handler_list;
356*232edc2fSDmitry Osipenko 		handler->blocking = true;
357*232edc2fSDmitry Osipenko 		break;
358*232edc2fSDmitry Osipenko 
359*232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF:
360*232edc2fSDmitry Osipenko 		handler->list = &power_off_handler_list;
361*232edc2fSDmitry Osipenko 		break;
362*232edc2fSDmitry Osipenko 
363*232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_RESTART:
364*232edc2fSDmitry Osipenko 		handler->list = &restart_handler_list;
365*232edc2fSDmitry Osipenko 		break;
366*232edc2fSDmitry Osipenko 
367*232edc2fSDmitry Osipenko 	default:
368*232edc2fSDmitry Osipenko 		kfree(handler);
369*232edc2fSDmitry Osipenko 		return ERR_PTR(-EINVAL);
370*232edc2fSDmitry Osipenko 	}
371*232edc2fSDmitry Osipenko 
372*232edc2fSDmitry Osipenko 	handler->nb.notifier_call = sys_off_notify;
373*232edc2fSDmitry Osipenko 	handler->nb.priority = priority;
374*232edc2fSDmitry Osipenko 	handler->sys_off_cb = callback;
375*232edc2fSDmitry Osipenko 	handler->cb_data = cb_data;
376*232edc2fSDmitry Osipenko 	handler->mode = mode;
377*232edc2fSDmitry Osipenko 
378*232edc2fSDmitry Osipenko 	if (handler->blocking) {
379*232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
380*232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register(handler->list,
381*232edc2fSDmitry Osipenko 							       &handler->nb);
382*232edc2fSDmitry Osipenko 		else
383*232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register_unique_prio(handler->list,
384*232edc2fSDmitry Osipenko 									   &handler->nb);
385*232edc2fSDmitry Osipenko 	} else {
386*232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
387*232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register(handler->list,
388*232edc2fSDmitry Osipenko 							     &handler->nb);
389*232edc2fSDmitry Osipenko 		else
390*232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register_unique_prio(handler->list,
391*232edc2fSDmitry Osipenko 									 &handler->nb);
392*232edc2fSDmitry Osipenko 	}
393*232edc2fSDmitry Osipenko 
394*232edc2fSDmitry Osipenko 	if (err) {
395*232edc2fSDmitry Osipenko 		kfree(handler);
396*232edc2fSDmitry Osipenko 		return ERR_PTR(err);
397*232edc2fSDmitry Osipenko 	}
398*232edc2fSDmitry Osipenko 
399*232edc2fSDmitry Osipenko 	return handler;
400*232edc2fSDmitry Osipenko }
401*232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(register_sys_off_handler);
402*232edc2fSDmitry Osipenko 
403*232edc2fSDmitry Osipenko /**
404*232edc2fSDmitry Osipenko  *	unregister_sys_off_handler - Unregister sys-off handler
405*232edc2fSDmitry Osipenko  *	@handler: Sys-off handler
406*232edc2fSDmitry Osipenko  *
407*232edc2fSDmitry Osipenko  *	Unregisters given sys-off handler.
408*232edc2fSDmitry Osipenko  */
409*232edc2fSDmitry Osipenko void unregister_sys_off_handler(struct sys_off_handler *handler)
410*232edc2fSDmitry Osipenko {
411*232edc2fSDmitry Osipenko 	int err;
412*232edc2fSDmitry Osipenko 
413*232edc2fSDmitry Osipenko 	if (!handler)
414*232edc2fSDmitry Osipenko 		return;
415*232edc2fSDmitry Osipenko 
416*232edc2fSDmitry Osipenko 	if (handler->blocking)
417*232edc2fSDmitry Osipenko 		err = blocking_notifier_chain_unregister(handler->list,
418*232edc2fSDmitry Osipenko 							 &handler->nb);
419*232edc2fSDmitry Osipenko 	else
420*232edc2fSDmitry Osipenko 		err = atomic_notifier_chain_unregister(handler->list,
421*232edc2fSDmitry Osipenko 						       &handler->nb);
422*232edc2fSDmitry Osipenko 
423*232edc2fSDmitry Osipenko 	/* sanity check, shall never happen */
424*232edc2fSDmitry Osipenko 	WARN_ON(err);
425*232edc2fSDmitry Osipenko 
426*232edc2fSDmitry Osipenko 	kfree(handler);
427*232edc2fSDmitry Osipenko }
428*232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
429*232edc2fSDmitry Osipenko 
430*232edc2fSDmitry Osipenko static void devm_unregister_sys_off_handler(void *data)
431*232edc2fSDmitry Osipenko {
432*232edc2fSDmitry Osipenko 	struct sys_off_handler *handler = data;
433*232edc2fSDmitry Osipenko 
434*232edc2fSDmitry Osipenko 	unregister_sys_off_handler(handler);
435*232edc2fSDmitry Osipenko }
436*232edc2fSDmitry Osipenko 
437*232edc2fSDmitry Osipenko /**
438*232edc2fSDmitry Osipenko  *	devm_register_sys_off_handler - Register sys-off handler
439*232edc2fSDmitry Osipenko  *	@dev: Device that registers handler
440*232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
441*232edc2fSDmitry Osipenko  *	@priority: Handler priority
442*232edc2fSDmitry Osipenko  *	@callback: Callback function
443*232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
444*232edc2fSDmitry Osipenko  *
445*232edc2fSDmitry Osipenko  *	Registers resource-managed sys-off handler.
446*232edc2fSDmitry Osipenko  *
447*232edc2fSDmitry Osipenko  *	Returns zero on success, or error code on failure.
448*232edc2fSDmitry Osipenko  */
449*232edc2fSDmitry Osipenko int devm_register_sys_off_handler(struct device *dev,
450*232edc2fSDmitry Osipenko 				  enum sys_off_mode mode,
451*232edc2fSDmitry Osipenko 				  int priority,
452*232edc2fSDmitry Osipenko 				  int (*callback)(struct sys_off_data *data),
453*232edc2fSDmitry Osipenko 				  void *cb_data)
454*232edc2fSDmitry Osipenko {
455*232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
456*232edc2fSDmitry Osipenko 
457*232edc2fSDmitry Osipenko 	handler = register_sys_off_handler(mode, priority, callback, cb_data);
458*232edc2fSDmitry Osipenko 	if (IS_ERR(handler))
459*232edc2fSDmitry Osipenko 		return PTR_ERR(handler);
460*232edc2fSDmitry Osipenko 
461*232edc2fSDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
462*232edc2fSDmitry Osipenko 					handler);
463*232edc2fSDmitry Osipenko }
464*232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
465*232edc2fSDmitry Osipenko 
46615d94b82SRobin Holt /**
46715d94b82SRobin Holt  *	kernel_power_off - power_off the system
46815d94b82SRobin Holt  *
46915d94b82SRobin Holt  *	Shutdown everything and perform a clean system power_off.
47015d94b82SRobin Holt  */
47115d94b82SRobin Holt void kernel_power_off(void)
47215d94b82SRobin Holt {
47315d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
47415d94b82SRobin Holt 	if (pm_power_off_prepare)
47515d94b82SRobin Holt 		pm_power_off_prepare();
47615d94b82SRobin Holt 	migrate_to_reboot_cpu();
47715d94b82SRobin Holt 	syscore_shutdown();
478972ee83dSRobin Holt 	pr_emerg("Power down\n");
4796d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
48015d94b82SRobin Holt 	machine_power_off();
48115d94b82SRobin Holt }
48215d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off);
48315d94b82SRobin Holt 
48455f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex);
48515d94b82SRobin Holt 
48615d94b82SRobin Holt /*
48715d94b82SRobin Holt  * Reboot system call: for obvious reasons only root may call it,
48815d94b82SRobin Holt  * and even root needs to set up some magic numbers in the registers
48915d94b82SRobin Holt  * so that some mistake won't make this reboot the whole machine.
49015d94b82SRobin Holt  * You can also set the meaning of the ctrl-alt-del-key here.
49115d94b82SRobin Holt  *
49215d94b82SRobin Holt  * reboot doesn't sync: do that yourself before calling this.
49315d94b82SRobin Holt  */
49415d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
49515d94b82SRobin Holt 		void __user *, arg)
49615d94b82SRobin Holt {
49715d94b82SRobin Holt 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
49815d94b82SRobin Holt 	char buffer[256];
49915d94b82SRobin Holt 	int ret = 0;
50015d94b82SRobin Holt 
50115d94b82SRobin Holt 	/* We only trust the superuser with rebooting the system. */
50215d94b82SRobin Holt 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
50315d94b82SRobin Holt 		return -EPERM;
50415d94b82SRobin Holt 
50515d94b82SRobin Holt 	/* For safety, we require "magic" arguments. */
50615d94b82SRobin Holt 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
50715d94b82SRobin Holt 			(magic2 != LINUX_REBOOT_MAGIC2 &&
50815d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2A &&
50915d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2B &&
51015d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2C))
51115d94b82SRobin Holt 		return -EINVAL;
51215d94b82SRobin Holt 
51315d94b82SRobin Holt 	/*
51415d94b82SRobin Holt 	 * If pid namespaces are enabled and the current task is in a child
51515d94b82SRobin Holt 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
51615d94b82SRobin Holt 	 * call do_exit().
51715d94b82SRobin Holt 	 */
51815d94b82SRobin Holt 	ret = reboot_pid_ns(pid_ns, cmd);
51915d94b82SRobin Holt 	if (ret)
52015d94b82SRobin Holt 		return ret;
52115d94b82SRobin Holt 
52215d94b82SRobin Holt 	/* Instead of trying to make the power_off code look like
52315d94b82SRobin Holt 	 * halt when pm_power_off is not set do it the easy way.
52415d94b82SRobin Holt 	 */
52515d94b82SRobin Holt 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
52615d94b82SRobin Holt 		cmd = LINUX_REBOOT_CMD_HALT;
52715d94b82SRobin Holt 
52855f2503cSPingfan Liu 	mutex_lock(&system_transition_mutex);
52915d94b82SRobin Holt 	switch (cmd) {
53015d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART:
53115d94b82SRobin Holt 		kernel_restart(NULL);
53215d94b82SRobin Holt 		break;
53315d94b82SRobin Holt 
53415d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_ON:
53515d94b82SRobin Holt 		C_A_D = 1;
53615d94b82SRobin Holt 		break;
53715d94b82SRobin Holt 
53815d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_OFF:
53915d94b82SRobin Holt 		C_A_D = 0;
54015d94b82SRobin Holt 		break;
54115d94b82SRobin Holt 
54215d94b82SRobin Holt 	case LINUX_REBOOT_CMD_HALT:
54315d94b82SRobin Holt 		kernel_halt();
54415d94b82SRobin Holt 		do_exit(0);
54515d94b82SRobin Holt 
54615d94b82SRobin Holt 	case LINUX_REBOOT_CMD_POWER_OFF:
54715d94b82SRobin Holt 		kernel_power_off();
54815d94b82SRobin Holt 		do_exit(0);
54915d94b82SRobin Holt 		break;
55015d94b82SRobin Holt 
55115d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART2:
552972ee83dSRobin Holt 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
553972ee83dSRobin Holt 		if (ret < 0) {
55415d94b82SRobin Holt 			ret = -EFAULT;
55515d94b82SRobin Holt 			break;
55615d94b82SRobin Holt 		}
55715d94b82SRobin Holt 		buffer[sizeof(buffer) - 1] = '\0';
55815d94b82SRobin Holt 
55915d94b82SRobin Holt 		kernel_restart(buffer);
56015d94b82SRobin Holt 		break;
56115d94b82SRobin Holt 
5622965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
56315d94b82SRobin Holt 	case LINUX_REBOOT_CMD_KEXEC:
56415d94b82SRobin Holt 		ret = kernel_kexec();
56515d94b82SRobin Holt 		break;
56615d94b82SRobin Holt #endif
56715d94b82SRobin Holt 
56815d94b82SRobin Holt #ifdef CONFIG_HIBERNATION
56915d94b82SRobin Holt 	case LINUX_REBOOT_CMD_SW_SUSPEND:
57015d94b82SRobin Holt 		ret = hibernate();
57115d94b82SRobin Holt 		break;
57215d94b82SRobin Holt #endif
57315d94b82SRobin Holt 
57415d94b82SRobin Holt 	default:
57515d94b82SRobin Holt 		ret = -EINVAL;
57615d94b82SRobin Holt 		break;
57715d94b82SRobin Holt 	}
57855f2503cSPingfan Liu 	mutex_unlock(&system_transition_mutex);
57915d94b82SRobin Holt 	return ret;
58015d94b82SRobin Holt }
58115d94b82SRobin Holt 
58215d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy)
58315d94b82SRobin Holt {
58415d94b82SRobin Holt 	kernel_restart(NULL);
58515d94b82SRobin Holt }
58615d94b82SRobin Holt 
58715d94b82SRobin Holt /*
58815d94b82SRobin Holt  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
58915d94b82SRobin Holt  * As it's called within an interrupt, it may NOT sync: the only choice
59015d94b82SRobin Holt  * is whether to reboot at once, or just ignore the ctrl-alt-del.
59115d94b82SRobin Holt  */
59215d94b82SRobin Holt void ctrl_alt_del(void)
59315d94b82SRobin Holt {
59415d94b82SRobin Holt 	static DECLARE_WORK(cad_work, deferred_cad);
59515d94b82SRobin Holt 
59615d94b82SRobin Holt 	if (C_A_D)
59715d94b82SRobin Holt 		schedule_work(&cad_work);
59815d94b82SRobin Holt 	else
59915d94b82SRobin Holt 		kill_cad_pid(SIGINT, 1);
60015d94b82SRobin Holt }
60115d94b82SRobin Holt 
60215d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
6037a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot";
60415d94b82SRobin Holt 
6057a54f46bSJoel Stanley static int run_cmd(const char *cmd)
60615d94b82SRobin Holt {
60715d94b82SRobin Holt 	char **argv;
60815d94b82SRobin Holt 	static char *envp[] = {
60915d94b82SRobin Holt 		"HOME=/",
61015d94b82SRobin Holt 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
61115d94b82SRobin Holt 		NULL
61215d94b82SRobin Holt 	};
61315d94b82SRobin Holt 	int ret;
6147a54f46bSJoel Stanley 	argv = argv_split(GFP_KERNEL, cmd, NULL);
61515d94b82SRobin Holt 	if (argv) {
61615d94b82SRobin Holt 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
61715d94b82SRobin Holt 		argv_free(argv);
61815d94b82SRobin Holt 	} else {
61915d94b82SRobin Holt 		ret = -ENOMEM;
62015d94b82SRobin Holt 	}
62115d94b82SRobin Holt 
6227a54f46bSJoel Stanley 	return ret;
6237a54f46bSJoel Stanley }
6247a54f46bSJoel Stanley 
6257a54f46bSJoel Stanley static int __orderly_reboot(void)
6267a54f46bSJoel Stanley {
6277a54f46bSJoel Stanley 	int ret;
6287a54f46bSJoel Stanley 
6297a54f46bSJoel Stanley 	ret = run_cmd(reboot_cmd);
6307a54f46bSJoel Stanley 
6317a54f46bSJoel Stanley 	if (ret) {
6327a54f46bSJoel Stanley 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
6337a54f46bSJoel Stanley 		emergency_sync();
6347a54f46bSJoel Stanley 		kernel_restart(NULL);
6357a54f46bSJoel Stanley 	}
6367a54f46bSJoel Stanley 
6377a54f46bSJoel Stanley 	return ret;
6387a54f46bSJoel Stanley }
6397a54f46bSJoel Stanley 
6407a54f46bSJoel Stanley static int __orderly_poweroff(bool force)
6417a54f46bSJoel Stanley {
6427a54f46bSJoel Stanley 	int ret;
6437a54f46bSJoel Stanley 
6447a54f46bSJoel Stanley 	ret = run_cmd(poweroff_cmd);
6457a54f46bSJoel Stanley 
64615d94b82SRobin Holt 	if (ret && force) {
647972ee83dSRobin Holt 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
6487a54f46bSJoel Stanley 
64915d94b82SRobin Holt 		/*
65015d94b82SRobin Holt 		 * I guess this should try to kick off some daemon to sync and
65115d94b82SRobin Holt 		 * poweroff asap.  Or not even bother syncing if we're doing an
65215d94b82SRobin Holt 		 * emergency shutdown?
65315d94b82SRobin Holt 		 */
65415d94b82SRobin Holt 		emergency_sync();
65515d94b82SRobin Holt 		kernel_power_off();
65615d94b82SRobin Holt 	}
65715d94b82SRobin Holt 
65815d94b82SRobin Holt 	return ret;
65915d94b82SRobin Holt }
66015d94b82SRobin Holt 
66115d94b82SRobin Holt static bool poweroff_force;
66215d94b82SRobin Holt 
66315d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work)
66415d94b82SRobin Holt {
66515d94b82SRobin Holt 	__orderly_poweroff(poweroff_force);
66615d94b82SRobin Holt }
66715d94b82SRobin Holt 
66815d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func);
66915d94b82SRobin Holt 
67015d94b82SRobin Holt /**
67115d94b82SRobin Holt  * orderly_poweroff - Trigger an orderly system poweroff
67215d94b82SRobin Holt  * @force: force poweroff if command execution fails
67315d94b82SRobin Holt  *
67415d94b82SRobin Holt  * This may be called from any context to trigger a system shutdown.
67515d94b82SRobin Holt  * If the orderly shutdown fails, it will force an immediate shutdown.
67615d94b82SRobin Holt  */
6777a54f46bSJoel Stanley void orderly_poweroff(bool force)
67815d94b82SRobin Holt {
67915d94b82SRobin Holt 	if (force) /* do not override the pending "true" */
68015d94b82SRobin Holt 		poweroff_force = true;
68115d94b82SRobin Holt 	schedule_work(&poweroff_work);
68215d94b82SRobin Holt }
68315d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff);
6841b3a5d02SRobin Holt 
6857a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work)
6867a54f46bSJoel Stanley {
6877a54f46bSJoel Stanley 	__orderly_reboot();
6887a54f46bSJoel Stanley }
6897a54f46bSJoel Stanley 
6907a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func);
6917a54f46bSJoel Stanley 
6927a54f46bSJoel Stanley /**
6937a54f46bSJoel Stanley  * orderly_reboot - Trigger an orderly system reboot
6947a54f46bSJoel Stanley  *
6957a54f46bSJoel Stanley  * This may be called from any context to trigger a system reboot.
6967a54f46bSJoel Stanley  * If the orderly reboot fails, it will force an immediate reboot.
6977a54f46bSJoel Stanley  */
6987a54f46bSJoel Stanley void orderly_reboot(void)
6997a54f46bSJoel Stanley {
7007a54f46bSJoel Stanley 	schedule_work(&reboot_work);
7017a54f46bSJoel Stanley }
7027a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot);
7037a54f46bSJoel Stanley 
704dfa19b11SMatti Vaittinen /**
705dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
706dfa19b11SMatti Vaittinen  * @work: work_struct associated with the emergency poweroff function
707dfa19b11SMatti Vaittinen  *
708dfa19b11SMatti Vaittinen  * This function is called in very critical situations to force
709dfa19b11SMatti Vaittinen  * a kernel poweroff after a configurable timeout value.
710dfa19b11SMatti Vaittinen  */
711dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work)
712dfa19b11SMatti Vaittinen {
713dfa19b11SMatti Vaittinen 	/*
714dfa19b11SMatti Vaittinen 	 * We have reached here after the emergency shutdown waiting period has
715dfa19b11SMatti Vaittinen 	 * expired. This means orderly_poweroff has not been able to shut off
716dfa19b11SMatti Vaittinen 	 * the system for some reason.
717dfa19b11SMatti Vaittinen 	 *
718dfa19b11SMatti Vaittinen 	 * Try to shut down the system immediately using kernel_power_off
719dfa19b11SMatti Vaittinen 	 * if populated
720dfa19b11SMatti Vaittinen 	 */
721dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
722dfa19b11SMatti Vaittinen 	kernel_power_off();
723dfa19b11SMatti Vaittinen 
724dfa19b11SMatti Vaittinen 	/*
725dfa19b11SMatti Vaittinen 	 * Worst of the worst case trigger emergency restart
726dfa19b11SMatti Vaittinen 	 */
727dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
728dfa19b11SMatti Vaittinen 	emergency_restart();
729dfa19b11SMatti Vaittinen }
730dfa19b11SMatti Vaittinen 
731dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
732dfa19b11SMatti Vaittinen 			    hw_failure_emergency_poweroff_func);
733dfa19b11SMatti Vaittinen 
734dfa19b11SMatti Vaittinen /**
735dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
736dfa19b11SMatti Vaittinen  *
737dfa19b11SMatti Vaittinen  * This may be called from any critical situation to trigger a system shutdown
738dfa19b11SMatti Vaittinen  * after a given period of time. If time is negative this is not scheduled.
739dfa19b11SMatti Vaittinen  */
740dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
741dfa19b11SMatti Vaittinen {
742dfa19b11SMatti Vaittinen 	if (poweroff_delay_ms <= 0)
743dfa19b11SMatti Vaittinen 		return;
744dfa19b11SMatti Vaittinen 	schedule_delayed_work(&hw_failure_emergency_poweroff_work,
745dfa19b11SMatti Vaittinen 			      msecs_to_jiffies(poweroff_delay_ms));
746dfa19b11SMatti Vaittinen }
747dfa19b11SMatti Vaittinen 
748dfa19b11SMatti Vaittinen /**
749dfa19b11SMatti Vaittinen  * hw_protection_shutdown - Trigger an emergency system poweroff
750dfa19b11SMatti Vaittinen  *
751dfa19b11SMatti Vaittinen  * @reason:		Reason of emergency shutdown to be printed.
752dfa19b11SMatti Vaittinen  * @ms_until_forced:	Time to wait for orderly shutdown before tiggering a
753dfa19b11SMatti Vaittinen  *			forced shudown. Negative value disables the forced
754dfa19b11SMatti Vaittinen  *			shutdown.
755dfa19b11SMatti Vaittinen  *
756dfa19b11SMatti Vaittinen  * Initiate an emergency system shutdown in order to protect hardware from
757dfa19b11SMatti Vaittinen  * further damage. Usage examples include a thermal protection or a voltage or
758dfa19b11SMatti Vaittinen  * current regulator failures.
759dfa19b11SMatti Vaittinen  * NOTE: The request is ignored if protection shutdown is already pending even
760dfa19b11SMatti Vaittinen  * if the previous request has given a large timeout for forced shutdown.
761dfa19b11SMatti Vaittinen  * Can be called from any context.
762dfa19b11SMatti Vaittinen  */
763dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced)
764dfa19b11SMatti Vaittinen {
765dfa19b11SMatti Vaittinen 	static atomic_t allow_proceed = ATOMIC_INIT(1);
766dfa19b11SMatti Vaittinen 
767dfa19b11SMatti Vaittinen 	pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
768dfa19b11SMatti Vaittinen 
769dfa19b11SMatti Vaittinen 	/* Shutdown should be initiated only once. */
770dfa19b11SMatti Vaittinen 	if (!atomic_dec_and_test(&allow_proceed))
771dfa19b11SMatti Vaittinen 		return;
772dfa19b11SMatti Vaittinen 
773dfa19b11SMatti Vaittinen 	/*
774dfa19b11SMatti Vaittinen 	 * Queue a backup emergency shutdown in the event of
775dfa19b11SMatti Vaittinen 	 * orderly_poweroff failure
776dfa19b11SMatti Vaittinen 	 */
777dfa19b11SMatti Vaittinen 	hw_failure_emergency_poweroff(ms_until_forced);
778dfa19b11SMatti Vaittinen 	orderly_poweroff(true);
779dfa19b11SMatti Vaittinen }
780dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown);
781dfa19b11SMatti Vaittinen 
7821b3a5d02SRobin Holt static int __init reboot_setup(char *str)
7831b3a5d02SRobin Holt {
7841b3a5d02SRobin Holt 	for (;;) {
785b287a25aSAaro Koskinen 		enum reboot_mode *mode;
786b287a25aSAaro Koskinen 
7871b3a5d02SRobin Holt 		/*
7881b3a5d02SRobin Holt 		 * Having anything passed on the command line via
7891b3a5d02SRobin Holt 		 * reboot= will cause us to disable DMI checking
7901b3a5d02SRobin Holt 		 * below.
7911b3a5d02SRobin Holt 		 */
7921b3a5d02SRobin Holt 		reboot_default = 0;
7931b3a5d02SRobin Holt 
794b287a25aSAaro Koskinen 		if (!strncmp(str, "panic_", 6)) {
795b287a25aSAaro Koskinen 			mode = &panic_reboot_mode;
796b287a25aSAaro Koskinen 			str += 6;
797b287a25aSAaro Koskinen 		} else {
798b287a25aSAaro Koskinen 			mode = &reboot_mode;
799b287a25aSAaro Koskinen 		}
800b287a25aSAaro Koskinen 
8011b3a5d02SRobin Holt 		switch (*str) {
8021b3a5d02SRobin Holt 		case 'w':
803b287a25aSAaro Koskinen 			*mode = REBOOT_WARM;
8041b3a5d02SRobin Holt 			break;
8051b3a5d02SRobin Holt 
8061b3a5d02SRobin Holt 		case 'c':
807b287a25aSAaro Koskinen 			*mode = REBOOT_COLD;
8081b3a5d02SRobin Holt 			break;
8091b3a5d02SRobin Holt 
8101b3a5d02SRobin Holt 		case 'h':
811b287a25aSAaro Koskinen 			*mode = REBOOT_HARD;
8121b3a5d02SRobin Holt 			break;
8131b3a5d02SRobin Holt 
8141b3a5d02SRobin Holt 		case 's':
815f9a90501SMatteo Croce 			/*
816f9a90501SMatteo Croce 			 * reboot_cpu is s[mp]#### with #### being the processor
817f9a90501SMatteo Croce 			 * to be used for rebooting. Skip 's' or 'smp' prefix.
818f9a90501SMatteo Croce 			 */
819f9a90501SMatteo Croce 			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
820f9a90501SMatteo Croce 
821f9a90501SMatteo Croce 			if (isdigit(str[0])) {
822f9a90501SMatteo Croce 				int cpu = simple_strtoul(str, NULL, 0);
823f9a90501SMatteo Croce 
824f9a90501SMatteo Croce 				if (cpu >= num_possible_cpus()) {
825df5b0ab3SMatteo Croce 					pr_err("Ignoring the CPU number in reboot= option. "
826df5b0ab3SMatteo Croce 					"CPU %d exceeds possible cpu number %d\n",
827f9a90501SMatteo Croce 					cpu, num_possible_cpus());
828df5b0ab3SMatteo Croce 					break;
829df5b0ab3SMatteo Croce 				}
830f9a90501SMatteo Croce 				reboot_cpu = cpu;
831f9a90501SMatteo Croce 			} else
832f9a90501SMatteo Croce 				*mode = REBOOT_SOFT;
8331b3a5d02SRobin Holt 			break;
8348b92c4ffSMatteo Croce 
8351b3a5d02SRobin Holt 		case 'g':
836b287a25aSAaro Koskinen 			*mode = REBOOT_GPIO;
8371b3a5d02SRobin Holt 			break;
8381b3a5d02SRobin Holt 
8391b3a5d02SRobin Holt 		case 'b':
8401b3a5d02SRobin Holt 		case 'a':
8411b3a5d02SRobin Holt 		case 'k':
8421b3a5d02SRobin Holt 		case 't':
8431b3a5d02SRobin Holt 		case 'e':
8441b3a5d02SRobin Holt 		case 'p':
8451b3a5d02SRobin Holt 			reboot_type = *str;
8461b3a5d02SRobin Holt 			break;
8471b3a5d02SRobin Holt 
8481b3a5d02SRobin Holt 		case 'f':
8491b3a5d02SRobin Holt 			reboot_force = 1;
8501b3a5d02SRobin Holt 			break;
8511b3a5d02SRobin Holt 		}
8521b3a5d02SRobin Holt 
8531b3a5d02SRobin Holt 		str = strchr(str, ',');
8541b3a5d02SRobin Holt 		if (str)
8551b3a5d02SRobin Holt 			str++;
8561b3a5d02SRobin Holt 		else
8571b3a5d02SRobin Holt 			break;
8581b3a5d02SRobin Holt 	}
8591b3a5d02SRobin Holt 	return 1;
8601b3a5d02SRobin Holt }
8611b3a5d02SRobin Holt __setup("reboot=", reboot_setup);
8622c622ed0SMatteo Croce 
8632c622ed0SMatteo Croce #ifdef CONFIG_SYSFS
8642c622ed0SMatteo Croce 
8652c622ed0SMatteo Croce #define REBOOT_COLD_STR		"cold"
8662c622ed0SMatteo Croce #define REBOOT_WARM_STR		"warm"
8672c622ed0SMatteo Croce #define REBOOT_HARD_STR		"hard"
8682c622ed0SMatteo Croce #define REBOOT_SOFT_STR		"soft"
8692c622ed0SMatteo Croce #define REBOOT_GPIO_STR		"gpio"
8702c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR	"undefined"
8712c622ed0SMatteo Croce 
8722c622ed0SMatteo Croce #define BOOT_TRIPLE_STR		"triple"
8732c622ed0SMatteo Croce #define BOOT_KBD_STR		"kbd"
8742c622ed0SMatteo Croce #define BOOT_BIOS_STR		"bios"
8752c622ed0SMatteo Croce #define BOOT_ACPI_STR		"acpi"
8762c622ed0SMatteo Croce #define BOOT_EFI_STR		"efi"
8770c5c0179SMatteo Croce #define BOOT_PCI_STR		"pci"
8782c622ed0SMatteo Croce 
8792c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
8802c622ed0SMatteo Croce {
8812c622ed0SMatteo Croce 	const char *val;
8822c622ed0SMatteo Croce 
8832c622ed0SMatteo Croce 	switch (reboot_mode) {
8842c622ed0SMatteo Croce 	case REBOOT_COLD:
8852c622ed0SMatteo Croce 		val = REBOOT_COLD_STR;
8862c622ed0SMatteo Croce 		break;
8872c622ed0SMatteo Croce 	case REBOOT_WARM:
8882c622ed0SMatteo Croce 		val = REBOOT_WARM_STR;
8892c622ed0SMatteo Croce 		break;
8902c622ed0SMatteo Croce 	case REBOOT_HARD:
8912c622ed0SMatteo Croce 		val = REBOOT_HARD_STR;
8922c622ed0SMatteo Croce 		break;
8932c622ed0SMatteo Croce 	case REBOOT_SOFT:
8942c622ed0SMatteo Croce 		val = REBOOT_SOFT_STR;
8952c622ed0SMatteo Croce 		break;
8962c622ed0SMatteo Croce 	case REBOOT_GPIO:
8972c622ed0SMatteo Croce 		val = REBOOT_GPIO_STR;
8982c622ed0SMatteo Croce 		break;
8992c622ed0SMatteo Croce 	default:
9002c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
9012c622ed0SMatteo Croce 	}
9022c622ed0SMatteo Croce 
9032c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
9042c622ed0SMatteo Croce }
9052c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
9062c622ed0SMatteo Croce 			  const char *buf, size_t count)
9072c622ed0SMatteo Croce {
9082c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
9092c622ed0SMatteo Croce 		return -EPERM;
9102c622ed0SMatteo Croce 
9112c622ed0SMatteo Croce 	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
9122c622ed0SMatteo Croce 		reboot_mode = REBOOT_COLD;
9132c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
9142c622ed0SMatteo Croce 		reboot_mode = REBOOT_WARM;
9152c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
9162c622ed0SMatteo Croce 		reboot_mode = REBOOT_HARD;
9172c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
9182c622ed0SMatteo Croce 		reboot_mode = REBOOT_SOFT;
9192c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
9202c622ed0SMatteo Croce 		reboot_mode = REBOOT_GPIO;
9212c622ed0SMatteo Croce 	else
9222c622ed0SMatteo Croce 		return -EINVAL;
9232c622ed0SMatteo Croce 
9241a9d079fSMatteo Croce 	reboot_default = 0;
9251a9d079fSMatteo Croce 
9262c622ed0SMatteo Croce 	return count;
9272c622ed0SMatteo Croce }
9282c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
9292c622ed0SMatteo Croce 
93040247e55SMatteo Croce #ifdef CONFIG_X86
93140247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
93240247e55SMatteo Croce {
93340247e55SMatteo Croce 	return sprintf(buf, "%d\n", reboot_force);
93440247e55SMatteo Croce }
93540247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
93640247e55SMatteo Croce 			  const char *buf, size_t count)
93740247e55SMatteo Croce {
93840247e55SMatteo Croce 	bool res;
93940247e55SMatteo Croce 
94040247e55SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
94140247e55SMatteo Croce 		return -EPERM;
94240247e55SMatteo Croce 
94340247e55SMatteo Croce 	if (kstrtobool(buf, &res))
94440247e55SMatteo Croce 		return -EINVAL;
94540247e55SMatteo Croce 
94640247e55SMatteo Croce 	reboot_default = 0;
94740247e55SMatteo Croce 	reboot_force = res;
94840247e55SMatteo Croce 
94940247e55SMatteo Croce 	return count;
95040247e55SMatteo Croce }
95140247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
95240247e55SMatteo Croce 
9532c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
9542c622ed0SMatteo Croce {
9552c622ed0SMatteo Croce 	const char *val;
9562c622ed0SMatteo Croce 
9572c622ed0SMatteo Croce 	switch (reboot_type) {
9582c622ed0SMatteo Croce 	case BOOT_TRIPLE:
9592c622ed0SMatteo Croce 		val = BOOT_TRIPLE_STR;
9602c622ed0SMatteo Croce 		break;
9612c622ed0SMatteo Croce 	case BOOT_KBD:
9622c622ed0SMatteo Croce 		val = BOOT_KBD_STR;
9632c622ed0SMatteo Croce 		break;
9642c622ed0SMatteo Croce 	case BOOT_BIOS:
9652c622ed0SMatteo Croce 		val = BOOT_BIOS_STR;
9662c622ed0SMatteo Croce 		break;
9672c622ed0SMatteo Croce 	case BOOT_ACPI:
9682c622ed0SMatteo Croce 		val = BOOT_ACPI_STR;
9692c622ed0SMatteo Croce 		break;
9702c622ed0SMatteo Croce 	case BOOT_EFI:
9712c622ed0SMatteo Croce 		val = BOOT_EFI_STR;
9722c622ed0SMatteo Croce 		break;
9732c622ed0SMatteo Croce 	case BOOT_CF9_FORCE:
9740c5c0179SMatteo Croce 		val = BOOT_PCI_STR;
9752c622ed0SMatteo Croce 		break;
9762c622ed0SMatteo Croce 	default:
9772c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
9782c622ed0SMatteo Croce 	}
9792c622ed0SMatteo Croce 
9802c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
9812c622ed0SMatteo Croce }
9822c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
9832c622ed0SMatteo Croce 			  const char *buf, size_t count)
9842c622ed0SMatteo Croce {
9852c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
9862c622ed0SMatteo Croce 		return -EPERM;
9872c622ed0SMatteo Croce 
9882c622ed0SMatteo Croce 	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
9892c622ed0SMatteo Croce 		reboot_type = BOOT_TRIPLE;
9902c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
9912c622ed0SMatteo Croce 		reboot_type = BOOT_KBD;
9922c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
9932c622ed0SMatteo Croce 		reboot_type = BOOT_BIOS;
9942c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
9952c622ed0SMatteo Croce 		reboot_type = BOOT_ACPI;
9962c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
9972c622ed0SMatteo Croce 		reboot_type = BOOT_EFI;
9980c5c0179SMatteo Croce 	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
9992c622ed0SMatteo Croce 		reboot_type = BOOT_CF9_FORCE;
10002c622ed0SMatteo Croce 	else
10012c622ed0SMatteo Croce 		return -EINVAL;
10022c622ed0SMatteo Croce 
10031a9d079fSMatteo Croce 	reboot_default = 0;
10041a9d079fSMatteo Croce 
10052c622ed0SMatteo Croce 	return count;
10062c622ed0SMatteo Croce }
10072c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
100840247e55SMatteo Croce #endif
10092c622ed0SMatteo Croce 
101040247e55SMatteo Croce #ifdef CONFIG_SMP
10112c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
10122c622ed0SMatteo Croce {
10132c622ed0SMatteo Croce 	return sprintf(buf, "%d\n", reboot_cpu);
10142c622ed0SMatteo Croce }
10152c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
10162c622ed0SMatteo Croce 			  const char *buf, size_t count)
10172c622ed0SMatteo Croce {
10182c622ed0SMatteo Croce 	unsigned int cpunum;
10192c622ed0SMatteo Croce 	int rc;
10202c622ed0SMatteo Croce 
10212c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
10222c622ed0SMatteo Croce 		return -EPERM;
10232c622ed0SMatteo Croce 
10242c622ed0SMatteo Croce 	rc = kstrtouint(buf, 0, &cpunum);
10252c622ed0SMatteo Croce 
10262c622ed0SMatteo Croce 	if (rc)
10272c622ed0SMatteo Croce 		return rc;
10282c622ed0SMatteo Croce 
10292c622ed0SMatteo Croce 	if (cpunum >= num_possible_cpus())
10302c622ed0SMatteo Croce 		return -ERANGE;
10312c622ed0SMatteo Croce 
10321a9d079fSMatteo Croce 	reboot_default = 0;
10332c622ed0SMatteo Croce 	reboot_cpu = cpunum;
10342c622ed0SMatteo Croce 
10352c622ed0SMatteo Croce 	return count;
10362c622ed0SMatteo Croce }
10372c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
103840247e55SMatteo Croce #endif
10392c622ed0SMatteo Croce 
10402c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = {
10412c622ed0SMatteo Croce 	&reboot_mode_attr.attr,
104240247e55SMatteo Croce #ifdef CONFIG_X86
10432c622ed0SMatteo Croce 	&reboot_force_attr.attr,
104440247e55SMatteo Croce 	&reboot_type_attr.attr,
104540247e55SMatteo Croce #endif
104640247e55SMatteo Croce #ifdef CONFIG_SMP
104740247e55SMatteo Croce 	&reboot_cpu_attr.attr,
104840247e55SMatteo Croce #endif
10492c622ed0SMatteo Croce 	NULL,
10502c622ed0SMatteo Croce };
10512c622ed0SMatteo Croce 
10522c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = {
10532c622ed0SMatteo Croce 	.attrs = reboot_attrs,
10542c622ed0SMatteo Croce };
10552c622ed0SMatteo Croce 
10562c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void)
10572c622ed0SMatteo Croce {
10582c622ed0SMatteo Croce 	struct kobject *reboot_kobj;
10592c622ed0SMatteo Croce 	int ret;
10602c622ed0SMatteo Croce 
10612c622ed0SMatteo Croce 	reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
10622c622ed0SMatteo Croce 	if (!reboot_kobj)
10632c622ed0SMatteo Croce 		return -ENOMEM;
10642c622ed0SMatteo Croce 
10652c622ed0SMatteo Croce 	ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
10662c622ed0SMatteo Croce 	if (ret) {
10672c622ed0SMatteo Croce 		kobject_put(reboot_kobj);
10682c622ed0SMatteo Croce 		return ret;
10692c622ed0SMatteo Croce 	}
10702c622ed0SMatteo Croce 
10712c622ed0SMatteo Croce 	return 0;
10722c622ed0SMatteo Croce }
10732c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init);
10742c622ed0SMatteo Croce 
10752c622ed0SMatteo Croce #endif
1076