xref: /openbmc/linux/kernel/reboot.c (revision b97d6790d03b763eca08847a9a5869a4291b9f9a)
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 
2606d17766Stangmeng static 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 
51232edc2fSDmitry Osipenko struct sys_off_handler {
52232edc2fSDmitry Osipenko 	struct notifier_block nb;
53232edc2fSDmitry Osipenko 	int (*sys_off_cb)(struct sys_off_data *data);
54232edc2fSDmitry Osipenko 	void *cb_data;
55232edc2fSDmitry Osipenko 	enum sys_off_mode mode;
56232edc2fSDmitry Osipenko 	bool blocking;
57232edc2fSDmitry Osipenko 	void *list;
58232edc2fSDmitry Osipenko };
5915d94b82SRobin Holt 
6015d94b82SRobin Holt /*
615d34b41aSDmitry Osipenko  * Temporary stub that prevents linkage failure while we're in process
625d34b41aSDmitry Osipenko  * of removing all uses of legacy pm_power_off() around the kernel.
635d34b41aSDmitry Osipenko  */
645d34b41aSDmitry Osipenko void __weak (*pm_power_off)(void);
6515d94b82SRobin Holt 
6615d94b82SRobin Holt /**
6715d94b82SRobin Holt  *	emergency_restart - reboot the system
6815d94b82SRobin Holt  *
6915d94b82SRobin Holt  *	Without shutting down any hardware or taking any locks
7015d94b82SRobin Holt  *	reboot the system.  This is called when we know we are in
7115d94b82SRobin Holt  *	trouble so this is our best effort to reboot.  This is
7215d94b82SRobin Holt  *	safe to call in interrupt context.
7315d94b82SRobin Holt  */
emergency_restart(void)7415d94b82SRobin Holt void emergency_restart(void)
7515d94b82SRobin Holt {
7615d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_EMERG);
77*b3d81d3eSBenjamin Bara 	system_state = SYSTEM_RESTART;
7815d94b82SRobin Holt 	machine_emergency_restart();
7915d94b82SRobin Holt }
8015d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart);
8115d94b82SRobin Holt 
kernel_restart_prepare(char * cmd)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  */
register_reboot_notifier(struct notifier_block * nb)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  */
unregister_reboot_notifier(struct notifier_block * nb)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 
devm_unregister_reboot_notifier(struct device * dev,void * res)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 
devm_register_reboot_notifier(struct device * dev,struct notifier_block * nb)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  */
register_restart_handler(struct notifier_block * nb)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  */
unregister_restart_handler(struct notifier_block * nb)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  */
do_kernel_restart(char * cmd)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 
migrate_to_reboot_cpu(void)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 
247e7fd8b68SKai-Heng Feng /*
248e7fd8b68SKai-Heng Feng  *	Notifier list for kernel code which wants to be called
249e7fd8b68SKai-Heng Feng  *	to prepare system for restart.
250e7fd8b68SKai-Heng Feng  */
251e7fd8b68SKai-Heng Feng static BLOCKING_NOTIFIER_HEAD(restart_prep_handler_list);
252e7fd8b68SKai-Heng Feng 
do_kernel_restart_prepare(void)253e7fd8b68SKai-Heng Feng static void do_kernel_restart_prepare(void)
254e7fd8b68SKai-Heng Feng {
255e7fd8b68SKai-Heng Feng 	blocking_notifier_call_chain(&restart_prep_handler_list, 0, NULL);
256e7fd8b68SKai-Heng Feng }
257e7fd8b68SKai-Heng Feng 
25815d94b82SRobin Holt /**
25915d94b82SRobin Holt  *	kernel_restart - reboot the system
26015d94b82SRobin Holt  *	@cmd: pointer to buffer containing command to execute for restart
26115d94b82SRobin Holt  *		or %NULL
26215d94b82SRobin Holt  *
26315d94b82SRobin Holt  *	Shutdown everything and perform a clean reboot.
26415d94b82SRobin Holt  *	This is not safe to call in interrupt context.
26515d94b82SRobin Holt  */
kernel_restart(char * cmd)26615d94b82SRobin Holt void kernel_restart(char *cmd)
26715d94b82SRobin Holt {
26815d94b82SRobin Holt 	kernel_restart_prepare(cmd);
269e7fd8b68SKai-Heng Feng 	do_kernel_restart_prepare();
27015d94b82SRobin Holt 	migrate_to_reboot_cpu();
27115d94b82SRobin Holt 	syscore_shutdown();
27215d94b82SRobin Holt 	if (!cmd)
273972ee83dSRobin Holt 		pr_emerg("Restarting system\n");
27415d94b82SRobin Holt 	else
275972ee83dSRobin Holt 		pr_emerg("Restarting system with command '%s'\n", cmd);
2766d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
27715d94b82SRobin Holt 	machine_restart(cmd);
27815d94b82SRobin Holt }
27915d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart);
28015d94b82SRobin Holt 
kernel_shutdown_prepare(enum system_states state)28115d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state)
28215d94b82SRobin Holt {
28315d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list,
28415d94b82SRobin Holt 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
28515d94b82SRobin Holt 	system_state = state;
28615d94b82SRobin Holt 	usermodehelper_disable();
28715d94b82SRobin Holt 	device_shutdown();
28815d94b82SRobin Holt }
28915d94b82SRobin Holt /**
29015d94b82SRobin Holt  *	kernel_halt - halt the system
29115d94b82SRobin Holt  *
29215d94b82SRobin Holt  *	Shutdown everything and perform a clean system halt.
29315d94b82SRobin Holt  */
kernel_halt(void)29415d94b82SRobin Holt void kernel_halt(void)
29515d94b82SRobin Holt {
29615d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_HALT);
29715d94b82SRobin Holt 	migrate_to_reboot_cpu();
29815d94b82SRobin Holt 	syscore_shutdown();
299972ee83dSRobin Holt 	pr_emerg("System halted\n");
3006d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
30115d94b82SRobin Holt 	machine_halt();
30215d94b82SRobin Holt }
30315d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt);
30415d94b82SRobin Holt 
305232edc2fSDmitry Osipenko /*
306232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
307232edc2fSDmitry Osipenko  *	to prepare system for power off.
308232edc2fSDmitry Osipenko  */
309232edc2fSDmitry Osipenko static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
310232edc2fSDmitry Osipenko 
311232edc2fSDmitry Osipenko /*
312232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
313232edc2fSDmitry Osipenko  *	to power off system.
314232edc2fSDmitry Osipenko  */
315232edc2fSDmitry Osipenko static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
316232edc2fSDmitry Osipenko 
sys_off_notify(struct notifier_block * nb,unsigned long mode,void * cmd)317232edc2fSDmitry Osipenko static int sys_off_notify(struct notifier_block *nb,
318232edc2fSDmitry Osipenko 			  unsigned long mode, void *cmd)
319232edc2fSDmitry Osipenko {
320232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
321232edc2fSDmitry Osipenko 	struct sys_off_data data = {};
322232edc2fSDmitry Osipenko 
323232edc2fSDmitry Osipenko 	handler = container_of(nb, struct sys_off_handler, nb);
324232edc2fSDmitry Osipenko 	data.cb_data = handler->cb_data;
325232edc2fSDmitry Osipenko 	data.mode = mode;
326232edc2fSDmitry Osipenko 	data.cmd = cmd;
327232edc2fSDmitry Osipenko 
328232edc2fSDmitry Osipenko 	return handler->sys_off_cb(&data);
329232edc2fSDmitry Osipenko }
330232edc2fSDmitry Osipenko 
331587b9bfeSDmitry Osipenko static struct sys_off_handler platform_sys_off_handler;
332587b9bfeSDmitry Osipenko 
alloc_sys_off_handler(int priority)333587b9bfeSDmitry Osipenko static struct sys_off_handler *alloc_sys_off_handler(int priority)
334587b9bfeSDmitry Osipenko {
335587b9bfeSDmitry Osipenko 	struct sys_off_handler *handler;
3362b8c612cSDmitry Osipenko 	gfp_t flags;
337587b9bfeSDmitry Osipenko 
338587b9bfeSDmitry Osipenko 	/*
339587b9bfeSDmitry Osipenko 	 * Platforms like m68k can't allocate sys_off handler dynamically
340587b9bfeSDmitry Osipenko 	 * at the early boot time because memory allocator isn't available yet.
341587b9bfeSDmitry Osipenko 	 */
342587b9bfeSDmitry Osipenko 	if (priority == SYS_OFF_PRIO_PLATFORM) {
343587b9bfeSDmitry Osipenko 		handler = &platform_sys_off_handler;
344587b9bfeSDmitry Osipenko 		if (handler->cb_data)
345587b9bfeSDmitry Osipenko 			return ERR_PTR(-EBUSY);
346587b9bfeSDmitry Osipenko 	} else {
3472b8c612cSDmitry Osipenko 		if (system_state > SYSTEM_RUNNING)
3482b8c612cSDmitry Osipenko 			flags = GFP_ATOMIC;
3492b8c612cSDmitry Osipenko 		else
3502b8c612cSDmitry Osipenko 			flags = GFP_KERNEL;
3512b8c612cSDmitry Osipenko 
3522b8c612cSDmitry Osipenko 		handler = kzalloc(sizeof(*handler), flags);
353587b9bfeSDmitry Osipenko 		if (!handler)
354587b9bfeSDmitry Osipenko 			return ERR_PTR(-ENOMEM);
355587b9bfeSDmitry Osipenko 	}
356587b9bfeSDmitry Osipenko 
357587b9bfeSDmitry Osipenko 	return handler;
358587b9bfeSDmitry Osipenko }
359587b9bfeSDmitry Osipenko 
free_sys_off_handler(struct sys_off_handler * handler)360587b9bfeSDmitry Osipenko static void free_sys_off_handler(struct sys_off_handler *handler)
361587b9bfeSDmitry Osipenko {
362587b9bfeSDmitry Osipenko 	if (handler == &platform_sys_off_handler)
363587b9bfeSDmitry Osipenko 		memset(handler, 0, sizeof(*handler));
364587b9bfeSDmitry Osipenko 	else
365587b9bfeSDmitry Osipenko 		kfree(handler);
366587b9bfeSDmitry Osipenko }
367587b9bfeSDmitry Osipenko 
368232edc2fSDmitry Osipenko /**
369232edc2fSDmitry Osipenko  *	register_sys_off_handler - Register sys-off handler
370232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
371232edc2fSDmitry Osipenko  *	@priority: Handler priority
372232edc2fSDmitry Osipenko  *	@callback: Callback function
373232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
374232edc2fSDmitry Osipenko  *
375232edc2fSDmitry Osipenko  *	Registers system power-off or restart handler that will be invoked
376232edc2fSDmitry Osipenko  *	at the step corresponding to the given sys-off mode. Handler's callback
377232edc2fSDmitry Osipenko  *	should return NOTIFY_DONE to permit execution of the next handler in
378232edc2fSDmitry Osipenko  *	the call chain or NOTIFY_STOP to break the chain (in error case for
379232edc2fSDmitry Osipenko  *	example).
380232edc2fSDmitry Osipenko  *
381232edc2fSDmitry Osipenko  *	Multiple handlers can be registered at the default priority level.
382232edc2fSDmitry Osipenko  *
383232edc2fSDmitry Osipenko  *	Only one handler can be registered at the non-default priority level,
384232edc2fSDmitry Osipenko  *	otherwise ERR_PTR(-EBUSY) is returned.
385232edc2fSDmitry Osipenko  *
386232edc2fSDmitry Osipenko  *	Returns a new instance of struct sys_off_handler on success, or
387232edc2fSDmitry Osipenko  *	an ERR_PTR()-encoded error code otherwise.
388232edc2fSDmitry Osipenko  */
389232edc2fSDmitry Osipenko struct sys_off_handler *
register_sys_off_handler(enum sys_off_mode mode,int priority,int (* callback)(struct sys_off_data * data),void * cb_data)390232edc2fSDmitry Osipenko register_sys_off_handler(enum sys_off_mode mode,
391232edc2fSDmitry Osipenko 			 int priority,
392232edc2fSDmitry Osipenko 			 int (*callback)(struct sys_off_data *data),
393232edc2fSDmitry Osipenko 			 void *cb_data)
394232edc2fSDmitry Osipenko {
395232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
396232edc2fSDmitry Osipenko 	int err;
397232edc2fSDmitry Osipenko 
398587b9bfeSDmitry Osipenko 	handler = alloc_sys_off_handler(priority);
399587b9bfeSDmitry Osipenko 	if (IS_ERR(handler))
400587b9bfeSDmitry Osipenko 		return handler;
401232edc2fSDmitry Osipenko 
402232edc2fSDmitry Osipenko 	switch (mode) {
403232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF_PREPARE:
404232edc2fSDmitry Osipenko 		handler->list = &power_off_prep_handler_list;
405232edc2fSDmitry Osipenko 		handler->blocking = true;
406232edc2fSDmitry Osipenko 		break;
407232edc2fSDmitry Osipenko 
408232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF:
409232edc2fSDmitry Osipenko 		handler->list = &power_off_handler_list;
410232edc2fSDmitry Osipenko 		break;
411232edc2fSDmitry Osipenko 
412e7fd8b68SKai-Heng Feng 	case SYS_OFF_MODE_RESTART_PREPARE:
413e7fd8b68SKai-Heng Feng 		handler->list = &restart_prep_handler_list;
414e7fd8b68SKai-Heng Feng 		handler->blocking = true;
415e7fd8b68SKai-Heng Feng 		break;
416e7fd8b68SKai-Heng Feng 
417232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_RESTART:
418232edc2fSDmitry Osipenko 		handler->list = &restart_handler_list;
419232edc2fSDmitry Osipenko 		break;
420232edc2fSDmitry Osipenko 
421232edc2fSDmitry Osipenko 	default:
422587b9bfeSDmitry Osipenko 		free_sys_off_handler(handler);
423232edc2fSDmitry Osipenko 		return ERR_PTR(-EINVAL);
424232edc2fSDmitry Osipenko 	}
425232edc2fSDmitry Osipenko 
426232edc2fSDmitry Osipenko 	handler->nb.notifier_call = sys_off_notify;
427232edc2fSDmitry Osipenko 	handler->nb.priority = priority;
428232edc2fSDmitry Osipenko 	handler->sys_off_cb = callback;
429232edc2fSDmitry Osipenko 	handler->cb_data = cb_data;
430232edc2fSDmitry Osipenko 	handler->mode = mode;
431232edc2fSDmitry Osipenko 
432232edc2fSDmitry Osipenko 	if (handler->blocking) {
433232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
434232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register(handler->list,
435232edc2fSDmitry Osipenko 							       &handler->nb);
436232edc2fSDmitry Osipenko 		else
437232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register_unique_prio(handler->list,
438232edc2fSDmitry Osipenko 									   &handler->nb);
439232edc2fSDmitry Osipenko 	} else {
440232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
441232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register(handler->list,
442232edc2fSDmitry Osipenko 							     &handler->nb);
443232edc2fSDmitry Osipenko 		else
444232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register_unique_prio(handler->list,
445232edc2fSDmitry Osipenko 									 &handler->nb);
446232edc2fSDmitry Osipenko 	}
447232edc2fSDmitry Osipenko 
448232edc2fSDmitry Osipenko 	if (err) {
449587b9bfeSDmitry Osipenko 		free_sys_off_handler(handler);
450232edc2fSDmitry Osipenko 		return ERR_PTR(err);
451232edc2fSDmitry Osipenko 	}
452232edc2fSDmitry Osipenko 
453232edc2fSDmitry Osipenko 	return handler;
454232edc2fSDmitry Osipenko }
455232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(register_sys_off_handler);
456232edc2fSDmitry Osipenko 
457232edc2fSDmitry Osipenko /**
458232edc2fSDmitry Osipenko  *	unregister_sys_off_handler - Unregister sys-off handler
459232edc2fSDmitry Osipenko  *	@handler: Sys-off handler
460232edc2fSDmitry Osipenko  *
461232edc2fSDmitry Osipenko  *	Unregisters given sys-off handler.
462232edc2fSDmitry Osipenko  */
unregister_sys_off_handler(struct sys_off_handler * handler)463232edc2fSDmitry Osipenko void unregister_sys_off_handler(struct sys_off_handler *handler)
464232edc2fSDmitry Osipenko {
465232edc2fSDmitry Osipenko 	int err;
466232edc2fSDmitry Osipenko 
4672b8c612cSDmitry Osipenko 	if (IS_ERR_OR_NULL(handler))
468232edc2fSDmitry Osipenko 		return;
469232edc2fSDmitry Osipenko 
470232edc2fSDmitry Osipenko 	if (handler->blocking)
471232edc2fSDmitry Osipenko 		err = blocking_notifier_chain_unregister(handler->list,
472232edc2fSDmitry Osipenko 							 &handler->nb);
473232edc2fSDmitry Osipenko 	else
474232edc2fSDmitry Osipenko 		err = atomic_notifier_chain_unregister(handler->list,
475232edc2fSDmitry Osipenko 						       &handler->nb);
476232edc2fSDmitry Osipenko 
477232edc2fSDmitry Osipenko 	/* sanity check, shall never happen */
478232edc2fSDmitry Osipenko 	WARN_ON(err);
479232edc2fSDmitry Osipenko 
480587b9bfeSDmitry Osipenko 	free_sys_off_handler(handler);
481232edc2fSDmitry Osipenko }
482232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
483232edc2fSDmitry Osipenko 
devm_unregister_sys_off_handler(void * data)484232edc2fSDmitry Osipenko static void devm_unregister_sys_off_handler(void *data)
485232edc2fSDmitry Osipenko {
486232edc2fSDmitry Osipenko 	struct sys_off_handler *handler = data;
487232edc2fSDmitry Osipenko 
488232edc2fSDmitry Osipenko 	unregister_sys_off_handler(handler);
489232edc2fSDmitry Osipenko }
490232edc2fSDmitry Osipenko 
491232edc2fSDmitry Osipenko /**
492232edc2fSDmitry Osipenko  *	devm_register_sys_off_handler - Register sys-off handler
493232edc2fSDmitry Osipenko  *	@dev: Device that registers handler
494232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
495232edc2fSDmitry Osipenko  *	@priority: Handler priority
496232edc2fSDmitry Osipenko  *	@callback: Callback function
497232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
498232edc2fSDmitry Osipenko  *
499232edc2fSDmitry Osipenko  *	Registers resource-managed sys-off handler.
500232edc2fSDmitry Osipenko  *
501232edc2fSDmitry Osipenko  *	Returns zero on success, or error code on failure.
502232edc2fSDmitry Osipenko  */
devm_register_sys_off_handler(struct device * dev,enum sys_off_mode mode,int priority,int (* callback)(struct sys_off_data * data),void * cb_data)503232edc2fSDmitry Osipenko int devm_register_sys_off_handler(struct device *dev,
504232edc2fSDmitry Osipenko 				  enum sys_off_mode mode,
505232edc2fSDmitry Osipenko 				  int priority,
506232edc2fSDmitry Osipenko 				  int (*callback)(struct sys_off_data *data),
507232edc2fSDmitry Osipenko 				  void *cb_data)
508232edc2fSDmitry Osipenko {
509232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
510232edc2fSDmitry Osipenko 
511232edc2fSDmitry Osipenko 	handler = register_sys_off_handler(mode, priority, callback, cb_data);
512232edc2fSDmitry Osipenko 	if (IS_ERR(handler))
513232edc2fSDmitry Osipenko 		return PTR_ERR(handler);
514232edc2fSDmitry Osipenko 
515232edc2fSDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
516232edc2fSDmitry Osipenko 					handler);
517232edc2fSDmitry Osipenko }
518232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
519232edc2fSDmitry Osipenko 
520d2c54153SDmitry Osipenko /**
521d2c54153SDmitry Osipenko  *	devm_register_power_off_handler - Register power-off handler
522d2c54153SDmitry Osipenko  *	@dev: Device that registers callback
523d2c54153SDmitry Osipenko  *	@callback: Callback function
524d2c54153SDmitry Osipenko  *	@cb_data: Callback's argument
525d2c54153SDmitry Osipenko  *
526d2c54153SDmitry Osipenko  *	Registers resource-managed sys-off handler with a default priority
527d2c54153SDmitry Osipenko  *	and using power-off mode.
528d2c54153SDmitry Osipenko  *
529d2c54153SDmitry Osipenko  *	Returns zero on success, or error code on failure.
530d2c54153SDmitry Osipenko  */
devm_register_power_off_handler(struct device * dev,int (* callback)(struct sys_off_data * data),void * cb_data)531d2c54153SDmitry Osipenko int devm_register_power_off_handler(struct device *dev,
532d2c54153SDmitry Osipenko 				    int (*callback)(struct sys_off_data *data),
533d2c54153SDmitry Osipenko 				    void *cb_data)
534d2c54153SDmitry Osipenko {
535d2c54153SDmitry Osipenko 	return devm_register_sys_off_handler(dev,
536d2c54153SDmitry Osipenko 					     SYS_OFF_MODE_POWER_OFF,
537d2c54153SDmitry Osipenko 					     SYS_OFF_PRIO_DEFAULT,
538d2c54153SDmitry Osipenko 					     callback, cb_data);
539d2c54153SDmitry Osipenko }
540d2c54153SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_power_off_handler);
541d2c54153SDmitry Osipenko 
5426779db97SDmitry Osipenko /**
5436779db97SDmitry Osipenko  *	devm_register_restart_handler - Register restart handler
5446779db97SDmitry Osipenko  *	@dev: Device that registers callback
5456779db97SDmitry Osipenko  *	@callback: Callback function
5466779db97SDmitry Osipenko  *	@cb_data: Callback's argument
5476779db97SDmitry Osipenko  *
5486779db97SDmitry Osipenko  *	Registers resource-managed sys-off handler with a default priority
5496779db97SDmitry Osipenko  *	and using restart mode.
5506779db97SDmitry Osipenko  *
5516779db97SDmitry Osipenko  *	Returns zero on success, or error code on failure.
5526779db97SDmitry Osipenko  */
devm_register_restart_handler(struct device * dev,int (* callback)(struct sys_off_data * data),void * cb_data)5536779db97SDmitry Osipenko int devm_register_restart_handler(struct device *dev,
5546779db97SDmitry Osipenko 				  int (*callback)(struct sys_off_data *data),
5556779db97SDmitry Osipenko 				  void *cb_data)
5566779db97SDmitry Osipenko {
5576779db97SDmitry Osipenko 	return devm_register_sys_off_handler(dev,
5586779db97SDmitry Osipenko 					     SYS_OFF_MODE_RESTART,
5596779db97SDmitry Osipenko 					     SYS_OFF_PRIO_DEFAULT,
5606779db97SDmitry Osipenko 					     callback, cb_data);
5616779db97SDmitry Osipenko }
5626779db97SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_restart_handler);
5636779db97SDmitry Osipenko 
564fb61375eSDmitry Osipenko static struct sys_off_handler *platform_power_off_handler;
565fb61375eSDmitry Osipenko 
platform_power_off_notify(struct sys_off_data * data)566fb61375eSDmitry Osipenko static int platform_power_off_notify(struct sys_off_data *data)
567fb61375eSDmitry Osipenko {
568fb61375eSDmitry Osipenko 	void (*platform_power_power_off_cb)(void) = data->cb_data;
569fb61375eSDmitry Osipenko 
570fb61375eSDmitry Osipenko 	platform_power_power_off_cb();
571fb61375eSDmitry Osipenko 
572fb61375eSDmitry Osipenko 	return NOTIFY_DONE;
573fb61375eSDmitry Osipenko }
574fb61375eSDmitry Osipenko 
575fb61375eSDmitry Osipenko /**
576fb61375eSDmitry Osipenko  *	register_platform_power_off - Register platform-level power-off callback
577fb61375eSDmitry Osipenko  *	@power_off: Power-off callback
578fb61375eSDmitry Osipenko  *
579fb61375eSDmitry Osipenko  *	Registers power-off callback that will be called as last step
580fb61375eSDmitry Osipenko  *	of the power-off sequence. This callback is expected to be invoked
581fb61375eSDmitry Osipenko  *	for the last resort. Only one platform power-off callback is allowed
582fb61375eSDmitry Osipenko  *	to be registered at a time.
583fb61375eSDmitry Osipenko  *
584fb61375eSDmitry Osipenko  *	Returns zero on success, or error code on failure.
585fb61375eSDmitry Osipenko  */
register_platform_power_off(void (* power_off)(void))586fb61375eSDmitry Osipenko int register_platform_power_off(void (*power_off)(void))
587fb61375eSDmitry Osipenko {
588fb61375eSDmitry Osipenko 	struct sys_off_handler *handler;
589fb61375eSDmitry Osipenko 
590fb61375eSDmitry Osipenko 	handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
591fb61375eSDmitry Osipenko 					   SYS_OFF_PRIO_PLATFORM,
592fb61375eSDmitry Osipenko 					   platform_power_off_notify,
593fb61375eSDmitry Osipenko 					   power_off);
594fb61375eSDmitry Osipenko 	if (IS_ERR(handler))
595fb61375eSDmitry Osipenko 		return PTR_ERR(handler);
596fb61375eSDmitry Osipenko 
597fb61375eSDmitry Osipenko 	platform_power_off_handler = handler;
598fb61375eSDmitry Osipenko 
599fb61375eSDmitry Osipenko 	return 0;
600fb61375eSDmitry Osipenko }
601fb61375eSDmitry Osipenko EXPORT_SYMBOL_GPL(register_platform_power_off);
602fb61375eSDmitry Osipenko 
603fb61375eSDmitry Osipenko /**
604fb61375eSDmitry Osipenko  *	unregister_platform_power_off - Unregister platform-level power-off callback
605fb61375eSDmitry Osipenko  *	@power_off: Power-off callback
606fb61375eSDmitry Osipenko  *
607fb61375eSDmitry Osipenko  *	Unregisters previously registered platform power-off callback.
608fb61375eSDmitry Osipenko  */
unregister_platform_power_off(void (* power_off)(void))609fb61375eSDmitry Osipenko void unregister_platform_power_off(void (*power_off)(void))
610fb61375eSDmitry Osipenko {
611fb61375eSDmitry Osipenko 	if (platform_power_off_handler &&
612fb61375eSDmitry Osipenko 	    platform_power_off_handler->cb_data == power_off) {
613fb61375eSDmitry Osipenko 		unregister_sys_off_handler(platform_power_off_handler);
614fb61375eSDmitry Osipenko 		platform_power_off_handler = NULL;
615fb61375eSDmitry Osipenko 	}
616fb61375eSDmitry Osipenko }
617fb61375eSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_platform_power_off);
618fb61375eSDmitry Osipenko 
legacy_pm_power_off(struct sys_off_data * data)6197b9a3de9SDmitry Osipenko static int legacy_pm_power_off(struct sys_off_data *data)
6207b9a3de9SDmitry Osipenko {
6217b9a3de9SDmitry Osipenko 	if (pm_power_off)
6227b9a3de9SDmitry Osipenko 		pm_power_off();
6237b9a3de9SDmitry Osipenko 
6247b9a3de9SDmitry Osipenko 	return NOTIFY_DONE;
6257b9a3de9SDmitry Osipenko }
6267b9a3de9SDmitry Osipenko 
do_kernel_power_off_prepare(void)6277b9a3de9SDmitry Osipenko static void do_kernel_power_off_prepare(void)
6287b9a3de9SDmitry Osipenko {
6297b9a3de9SDmitry Osipenko 	blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
6307b9a3de9SDmitry Osipenko }
6317b9a3de9SDmitry Osipenko 
63215d94b82SRobin Holt /**
6332b6aa733SDmitry Osipenko  *	do_kernel_power_off - Execute kernel power-off handler call chain
6342b6aa733SDmitry Osipenko  *
6352b6aa733SDmitry Osipenko  *	Expected to be called as last step of the power-off sequence.
6362b6aa733SDmitry Osipenko  *
6372b6aa733SDmitry Osipenko  *	Powers off the system immediately if a power-off handler function has
6382b6aa733SDmitry Osipenko  *	been registered. Otherwise does nothing.
6392b6aa733SDmitry Osipenko  */
do_kernel_power_off(void)6402b6aa733SDmitry Osipenko void do_kernel_power_off(void)
6412b6aa733SDmitry Osipenko {
6422b8c612cSDmitry Osipenko 	struct sys_off_handler *sys_off = NULL;
6432b8c612cSDmitry Osipenko 
6442b8c612cSDmitry Osipenko 	/*
6452b8c612cSDmitry Osipenko 	 * Register sys-off handlers for legacy PM callback. This allows
6462b8c612cSDmitry Osipenko 	 * legacy PM callbacks temporary co-exist with the new sys-off API.
6472b8c612cSDmitry Osipenko 	 *
6482b8c612cSDmitry Osipenko 	 * TODO: Remove legacy handlers once all legacy PM users will be
6492b8c612cSDmitry Osipenko 	 *       switched to the sys-off based APIs.
6502b8c612cSDmitry Osipenko 	 */
6512b8c612cSDmitry Osipenko 	if (pm_power_off)
6522b8c612cSDmitry Osipenko 		sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
6532b8c612cSDmitry Osipenko 						   SYS_OFF_PRIO_DEFAULT,
6542b8c612cSDmitry Osipenko 						   legacy_pm_power_off, NULL);
6552b8c612cSDmitry Osipenko 
6562b6aa733SDmitry Osipenko 	atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
6572b8c612cSDmitry Osipenko 
6582b8c612cSDmitry Osipenko 	unregister_sys_off_handler(sys_off);
6592b6aa733SDmitry Osipenko }
6602b6aa733SDmitry Osipenko 
6612b6aa733SDmitry Osipenko /**
6620e2110d2SDmitry Osipenko  *	kernel_can_power_off - check whether system can be powered off
6630e2110d2SDmitry Osipenko  *
6640e2110d2SDmitry Osipenko  *	Returns true if power-off handler is registered and system can be
6650e2110d2SDmitry Osipenko  *	powered off, false otherwise.
6660e2110d2SDmitry Osipenko  */
kernel_can_power_off(void)6670e2110d2SDmitry Osipenko bool kernel_can_power_off(void)
6680e2110d2SDmitry Osipenko {
6692b8c612cSDmitry Osipenko 	return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
6702b8c612cSDmitry Osipenko 		pm_power_off;
6710e2110d2SDmitry Osipenko }
6720e2110d2SDmitry Osipenko EXPORT_SYMBOL_GPL(kernel_can_power_off);
6730e2110d2SDmitry Osipenko 
67415d94b82SRobin Holt /**
67515d94b82SRobin Holt  *	kernel_power_off - power_off the system
67615d94b82SRobin Holt  *
67715d94b82SRobin Holt  *	Shutdown everything and perform a clean system power_off.
67815d94b82SRobin Holt  */
kernel_power_off(void)67915d94b82SRobin Holt void kernel_power_off(void)
68015d94b82SRobin Holt {
68115d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
6827b9a3de9SDmitry Osipenko 	do_kernel_power_off_prepare();
68315d94b82SRobin Holt 	migrate_to_reboot_cpu();
68415d94b82SRobin Holt 	syscore_shutdown();
685972ee83dSRobin Holt 	pr_emerg("Power down\n");
6866d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
68715d94b82SRobin Holt 	machine_power_off();
68815d94b82SRobin Holt }
68915d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off);
69015d94b82SRobin Holt 
69155f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex);
69215d94b82SRobin Holt 
69315d94b82SRobin Holt /*
69415d94b82SRobin Holt  * Reboot system call: for obvious reasons only root may call it,
69515d94b82SRobin Holt  * and even root needs to set up some magic numbers in the registers
69615d94b82SRobin Holt  * so that some mistake won't make this reboot the whole machine.
69715d94b82SRobin Holt  * You can also set the meaning of the ctrl-alt-del-key here.
69815d94b82SRobin Holt  *
69915d94b82SRobin Holt  * reboot doesn't sync: do that yourself before calling this.
70015d94b82SRobin Holt  */
SYSCALL_DEFINE4(reboot,int,magic1,int,magic2,unsigned int,cmd,void __user *,arg)70115d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
70215d94b82SRobin Holt 		void __user *, arg)
70315d94b82SRobin Holt {
70415d94b82SRobin Holt 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
70515d94b82SRobin Holt 	char buffer[256];
70615d94b82SRobin Holt 	int ret = 0;
70715d94b82SRobin Holt 
70815d94b82SRobin Holt 	/* We only trust the superuser with rebooting the system. */
70915d94b82SRobin Holt 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
71015d94b82SRobin Holt 		return -EPERM;
71115d94b82SRobin Holt 
71215d94b82SRobin Holt 	/* For safety, we require "magic" arguments. */
71315d94b82SRobin Holt 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
71415d94b82SRobin Holt 			(magic2 != LINUX_REBOOT_MAGIC2 &&
71515d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2A &&
71615d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2B &&
71715d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2C))
71815d94b82SRobin Holt 		return -EINVAL;
71915d94b82SRobin Holt 
72015d94b82SRobin Holt 	/*
72115d94b82SRobin Holt 	 * If pid namespaces are enabled and the current task is in a child
72215d94b82SRobin Holt 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
72315d94b82SRobin Holt 	 * call do_exit().
72415d94b82SRobin Holt 	 */
72515d94b82SRobin Holt 	ret = reboot_pid_ns(pid_ns, cmd);
72615d94b82SRobin Holt 	if (ret)
72715d94b82SRobin Holt 		return ret;
72815d94b82SRobin Holt 
72915d94b82SRobin Holt 	/* Instead of trying to make the power_off code look like
73015d94b82SRobin Holt 	 * halt when pm_power_off is not set do it the easy way.
73115d94b82SRobin Holt 	 */
7320e2110d2SDmitry Osipenko 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
73315d94b82SRobin Holt 		cmd = LINUX_REBOOT_CMD_HALT;
73415d94b82SRobin Holt 
73555f2503cSPingfan Liu 	mutex_lock(&system_transition_mutex);
73615d94b82SRobin Holt 	switch (cmd) {
73715d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART:
73815d94b82SRobin Holt 		kernel_restart(NULL);
73915d94b82SRobin Holt 		break;
74015d94b82SRobin Holt 
74115d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_ON:
74215d94b82SRobin Holt 		C_A_D = 1;
74315d94b82SRobin Holt 		break;
74415d94b82SRobin Holt 
74515d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_OFF:
74615d94b82SRobin Holt 		C_A_D = 0;
74715d94b82SRobin Holt 		break;
74815d94b82SRobin Holt 
74915d94b82SRobin Holt 	case LINUX_REBOOT_CMD_HALT:
75015d94b82SRobin Holt 		kernel_halt();
75115d94b82SRobin Holt 		do_exit(0);
75215d94b82SRobin Holt 
75315d94b82SRobin Holt 	case LINUX_REBOOT_CMD_POWER_OFF:
75415d94b82SRobin Holt 		kernel_power_off();
75515d94b82SRobin Holt 		do_exit(0);
75615d94b82SRobin Holt 		break;
75715d94b82SRobin Holt 
75815d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART2:
759972ee83dSRobin Holt 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
760972ee83dSRobin Holt 		if (ret < 0) {
76115d94b82SRobin Holt 			ret = -EFAULT;
76215d94b82SRobin Holt 			break;
76315d94b82SRobin Holt 		}
76415d94b82SRobin Holt 		buffer[sizeof(buffer) - 1] = '\0';
76515d94b82SRobin Holt 
76615d94b82SRobin Holt 		kernel_restart(buffer);
76715d94b82SRobin Holt 		break;
76815d94b82SRobin Holt 
7692965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
77015d94b82SRobin Holt 	case LINUX_REBOOT_CMD_KEXEC:
77115d94b82SRobin Holt 		ret = kernel_kexec();
77215d94b82SRobin Holt 		break;
77315d94b82SRobin Holt #endif
77415d94b82SRobin Holt 
77515d94b82SRobin Holt #ifdef CONFIG_HIBERNATION
77615d94b82SRobin Holt 	case LINUX_REBOOT_CMD_SW_SUSPEND:
77715d94b82SRobin Holt 		ret = hibernate();
77815d94b82SRobin Holt 		break;
77915d94b82SRobin Holt #endif
78015d94b82SRobin Holt 
78115d94b82SRobin Holt 	default:
78215d94b82SRobin Holt 		ret = -EINVAL;
78315d94b82SRobin Holt 		break;
78415d94b82SRobin Holt 	}
78555f2503cSPingfan Liu 	mutex_unlock(&system_transition_mutex);
78615d94b82SRobin Holt 	return ret;
78715d94b82SRobin Holt }
78815d94b82SRobin Holt 
deferred_cad(struct work_struct * dummy)78915d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy)
79015d94b82SRobin Holt {
79115d94b82SRobin Holt 	kernel_restart(NULL);
79215d94b82SRobin Holt }
79315d94b82SRobin Holt 
79415d94b82SRobin Holt /*
79515d94b82SRobin Holt  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
79615d94b82SRobin Holt  * As it's called within an interrupt, it may NOT sync: the only choice
79715d94b82SRobin Holt  * is whether to reboot at once, or just ignore the ctrl-alt-del.
79815d94b82SRobin Holt  */
ctrl_alt_del(void)79915d94b82SRobin Holt void ctrl_alt_del(void)
80015d94b82SRobin Holt {
80115d94b82SRobin Holt 	static DECLARE_WORK(cad_work, deferred_cad);
80215d94b82SRobin Holt 
80315d94b82SRobin Holt 	if (C_A_D)
80415d94b82SRobin Holt 		schedule_work(&cad_work);
80515d94b82SRobin Holt 	else
80615d94b82SRobin Holt 		kill_cad_pid(SIGINT, 1);
80715d94b82SRobin Holt }
80815d94b82SRobin Holt 
80906d17766Stangmeng #define POWEROFF_CMD_PATH_LEN  256
81006d17766Stangmeng static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
8117a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot";
81215d94b82SRobin Holt 
run_cmd(const char * cmd)8137a54f46bSJoel Stanley static int run_cmd(const char *cmd)
81415d94b82SRobin Holt {
81515d94b82SRobin Holt 	char **argv;
81615d94b82SRobin Holt 	static char *envp[] = {
81715d94b82SRobin Holt 		"HOME=/",
81815d94b82SRobin Holt 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
81915d94b82SRobin Holt 		NULL
82015d94b82SRobin Holt 	};
82115d94b82SRobin Holt 	int ret;
8227a54f46bSJoel Stanley 	argv = argv_split(GFP_KERNEL, cmd, NULL);
82315d94b82SRobin Holt 	if (argv) {
82415d94b82SRobin Holt 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
82515d94b82SRobin Holt 		argv_free(argv);
82615d94b82SRobin Holt 	} else {
82715d94b82SRobin Holt 		ret = -ENOMEM;
82815d94b82SRobin Holt 	}
82915d94b82SRobin Holt 
8307a54f46bSJoel Stanley 	return ret;
8317a54f46bSJoel Stanley }
8327a54f46bSJoel Stanley 
__orderly_reboot(void)8337a54f46bSJoel Stanley static int __orderly_reboot(void)
8347a54f46bSJoel Stanley {
8357a54f46bSJoel Stanley 	int ret;
8367a54f46bSJoel Stanley 
8377a54f46bSJoel Stanley 	ret = run_cmd(reboot_cmd);
8387a54f46bSJoel Stanley 
8397a54f46bSJoel Stanley 	if (ret) {
8407a54f46bSJoel Stanley 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
8417a54f46bSJoel Stanley 		emergency_sync();
8427a54f46bSJoel Stanley 		kernel_restart(NULL);
8437a54f46bSJoel Stanley 	}
8447a54f46bSJoel Stanley 
8457a54f46bSJoel Stanley 	return ret;
8467a54f46bSJoel Stanley }
8477a54f46bSJoel Stanley 
__orderly_poweroff(bool force)8487a54f46bSJoel Stanley static int __orderly_poweroff(bool force)
8497a54f46bSJoel Stanley {
8507a54f46bSJoel Stanley 	int ret;
8517a54f46bSJoel Stanley 
8527a54f46bSJoel Stanley 	ret = run_cmd(poweroff_cmd);
8537a54f46bSJoel Stanley 
85415d94b82SRobin Holt 	if (ret && force) {
855972ee83dSRobin Holt 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
8567a54f46bSJoel Stanley 
85715d94b82SRobin Holt 		/*
85815d94b82SRobin Holt 		 * I guess this should try to kick off some daemon to sync and
85915d94b82SRobin Holt 		 * poweroff asap.  Or not even bother syncing if we're doing an
86015d94b82SRobin Holt 		 * emergency shutdown?
86115d94b82SRobin Holt 		 */
86215d94b82SRobin Holt 		emergency_sync();
86315d94b82SRobin Holt 		kernel_power_off();
86415d94b82SRobin Holt 	}
86515d94b82SRobin Holt 
86615d94b82SRobin Holt 	return ret;
86715d94b82SRobin Holt }
86815d94b82SRobin Holt 
86915d94b82SRobin Holt static bool poweroff_force;
87015d94b82SRobin Holt 
poweroff_work_func(struct work_struct * work)87115d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work)
87215d94b82SRobin Holt {
87315d94b82SRobin Holt 	__orderly_poweroff(poweroff_force);
87415d94b82SRobin Holt }
87515d94b82SRobin Holt 
87615d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func);
87715d94b82SRobin Holt 
87815d94b82SRobin Holt /**
87915d94b82SRobin Holt  * orderly_poweroff - Trigger an orderly system poweroff
88015d94b82SRobin Holt  * @force: force poweroff if command execution fails
88115d94b82SRobin Holt  *
88215d94b82SRobin Holt  * This may be called from any context to trigger a system shutdown.
88315d94b82SRobin Holt  * If the orderly shutdown fails, it will force an immediate shutdown.
88415d94b82SRobin Holt  */
orderly_poweroff(bool force)8857a54f46bSJoel Stanley void orderly_poweroff(bool force)
88615d94b82SRobin Holt {
88715d94b82SRobin Holt 	if (force) /* do not override the pending "true" */
88815d94b82SRobin Holt 		poweroff_force = true;
88915d94b82SRobin Holt 	schedule_work(&poweroff_work);
89015d94b82SRobin Holt }
89115d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff);
8921b3a5d02SRobin Holt 
reboot_work_func(struct work_struct * work)8937a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work)
8947a54f46bSJoel Stanley {
8957a54f46bSJoel Stanley 	__orderly_reboot();
8967a54f46bSJoel Stanley }
8977a54f46bSJoel Stanley 
8987a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func);
8997a54f46bSJoel Stanley 
9007a54f46bSJoel Stanley /**
9017a54f46bSJoel Stanley  * orderly_reboot - Trigger an orderly system reboot
9027a54f46bSJoel Stanley  *
9037a54f46bSJoel Stanley  * This may be called from any context to trigger a system reboot.
9047a54f46bSJoel Stanley  * If the orderly reboot fails, it will force an immediate reboot.
9057a54f46bSJoel Stanley  */
orderly_reboot(void)9067a54f46bSJoel Stanley void orderly_reboot(void)
9077a54f46bSJoel Stanley {
9087a54f46bSJoel Stanley 	schedule_work(&reboot_work);
9097a54f46bSJoel Stanley }
9107a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot);
9117a54f46bSJoel Stanley 
912dfa19b11SMatti Vaittinen /**
913dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
914dfa19b11SMatti Vaittinen  * @work: work_struct associated with the emergency poweroff function
915dfa19b11SMatti Vaittinen  *
916dfa19b11SMatti Vaittinen  * This function is called in very critical situations to force
917dfa19b11SMatti Vaittinen  * a kernel poweroff after a configurable timeout value.
918dfa19b11SMatti Vaittinen  */
hw_failure_emergency_poweroff_func(struct work_struct * work)919dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work)
920dfa19b11SMatti Vaittinen {
921dfa19b11SMatti Vaittinen 	/*
922dfa19b11SMatti Vaittinen 	 * We have reached here after the emergency shutdown waiting period has
923dfa19b11SMatti Vaittinen 	 * expired. This means orderly_poweroff has not been able to shut off
924dfa19b11SMatti Vaittinen 	 * the system for some reason.
925dfa19b11SMatti Vaittinen 	 *
926dfa19b11SMatti Vaittinen 	 * Try to shut down the system immediately using kernel_power_off
927dfa19b11SMatti Vaittinen 	 * if populated
928dfa19b11SMatti Vaittinen 	 */
929dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
930dfa19b11SMatti Vaittinen 	kernel_power_off();
931dfa19b11SMatti Vaittinen 
932dfa19b11SMatti Vaittinen 	/*
933dfa19b11SMatti Vaittinen 	 * Worst of the worst case trigger emergency restart
934dfa19b11SMatti Vaittinen 	 */
935dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
936dfa19b11SMatti Vaittinen 	emergency_restart();
937dfa19b11SMatti Vaittinen }
938dfa19b11SMatti Vaittinen 
939dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
940dfa19b11SMatti Vaittinen 			    hw_failure_emergency_poweroff_func);
941dfa19b11SMatti Vaittinen 
942dfa19b11SMatti Vaittinen /**
943dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
944dfa19b11SMatti Vaittinen  *
945dfa19b11SMatti Vaittinen  * This may be called from any critical situation to trigger a system shutdown
946dfa19b11SMatti Vaittinen  * after a given period of time. If time is negative this is not scheduled.
947dfa19b11SMatti Vaittinen  */
hw_failure_emergency_poweroff(int poweroff_delay_ms)948dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
949dfa19b11SMatti Vaittinen {
950dfa19b11SMatti Vaittinen 	if (poweroff_delay_ms <= 0)
951dfa19b11SMatti Vaittinen 		return;
952dfa19b11SMatti Vaittinen 	schedule_delayed_work(&hw_failure_emergency_poweroff_work,
953dfa19b11SMatti Vaittinen 			      msecs_to_jiffies(poweroff_delay_ms));
954dfa19b11SMatti Vaittinen }
955dfa19b11SMatti Vaittinen 
956dfa19b11SMatti Vaittinen /**
957dfa19b11SMatti Vaittinen  * hw_protection_shutdown - Trigger an emergency system poweroff
958dfa19b11SMatti Vaittinen  *
959dfa19b11SMatti Vaittinen  * @reason:		Reason of emergency shutdown to be printed.
960dfa19b11SMatti Vaittinen  * @ms_until_forced:	Time to wait for orderly shutdown before tiggering a
961dfa19b11SMatti Vaittinen  *			forced shudown. Negative value disables the forced
962dfa19b11SMatti Vaittinen  *			shutdown.
963dfa19b11SMatti Vaittinen  *
964dfa19b11SMatti Vaittinen  * Initiate an emergency system shutdown in order to protect hardware from
965dfa19b11SMatti Vaittinen  * further damage. Usage examples include a thermal protection or a voltage or
966dfa19b11SMatti Vaittinen  * current regulator failures.
967dfa19b11SMatti Vaittinen  * NOTE: The request is ignored if protection shutdown is already pending even
968dfa19b11SMatti Vaittinen  * if the previous request has given a large timeout for forced shutdown.
969dfa19b11SMatti Vaittinen  * Can be called from any context.
970dfa19b11SMatti Vaittinen  */
hw_protection_shutdown(const char * reason,int ms_until_forced)971dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced)
972dfa19b11SMatti Vaittinen {
973dfa19b11SMatti Vaittinen 	static atomic_t allow_proceed = ATOMIC_INIT(1);
974dfa19b11SMatti Vaittinen 
975dfa19b11SMatti Vaittinen 	pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
976dfa19b11SMatti Vaittinen 
977dfa19b11SMatti Vaittinen 	/* Shutdown should be initiated only once. */
978dfa19b11SMatti Vaittinen 	if (!atomic_dec_and_test(&allow_proceed))
97907a22b61SPetr Mladek 		return;
980dfa19b11SMatti Vaittinen 
981dfa19b11SMatti Vaittinen 	/*
982dfa19b11SMatti Vaittinen 	 * Queue a backup emergency shutdown in the event of
983dfa19b11SMatti Vaittinen 	 * orderly_poweroff failure
984dfa19b11SMatti Vaittinen 	 */
985dfa19b11SMatti Vaittinen 	hw_failure_emergency_poweroff(ms_until_forced);
986dfa19b11SMatti Vaittinen 	orderly_poweroff(true);
987dfa19b11SMatti Vaittinen }
988dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown);
989dfa19b11SMatti Vaittinen 
reboot_setup(char * str)9901b3a5d02SRobin Holt static int __init reboot_setup(char *str)
9911b3a5d02SRobin Holt {
9921b3a5d02SRobin Holt 	for (;;) {
993b287a25aSAaro Koskinen 		enum reboot_mode *mode;
994b287a25aSAaro Koskinen 
9951b3a5d02SRobin Holt 		/*
9961b3a5d02SRobin Holt 		 * Having anything passed on the command line via
9971b3a5d02SRobin Holt 		 * reboot= will cause us to disable DMI checking
9981b3a5d02SRobin Holt 		 * below.
9991b3a5d02SRobin Holt 		 */
10001b3a5d02SRobin Holt 		reboot_default = 0;
10011b3a5d02SRobin Holt 
1002b287a25aSAaro Koskinen 		if (!strncmp(str, "panic_", 6)) {
1003b287a25aSAaro Koskinen 			mode = &panic_reboot_mode;
1004b287a25aSAaro Koskinen 			str += 6;
1005b287a25aSAaro Koskinen 		} else {
1006b287a25aSAaro Koskinen 			mode = &reboot_mode;
1007b287a25aSAaro Koskinen 		}
1008b287a25aSAaro Koskinen 
10091b3a5d02SRobin Holt 		switch (*str) {
10101b3a5d02SRobin Holt 		case 'w':
1011b287a25aSAaro Koskinen 			*mode = REBOOT_WARM;
10121b3a5d02SRobin Holt 			break;
10131b3a5d02SRobin Holt 
10141b3a5d02SRobin Holt 		case 'c':
1015b287a25aSAaro Koskinen 			*mode = REBOOT_COLD;
10161b3a5d02SRobin Holt 			break;
10171b3a5d02SRobin Holt 
10181b3a5d02SRobin Holt 		case 'h':
1019b287a25aSAaro Koskinen 			*mode = REBOOT_HARD;
10201b3a5d02SRobin Holt 			break;
10211b3a5d02SRobin Holt 
10221b3a5d02SRobin Holt 		case 's':
1023f9a90501SMatteo Croce 			/*
1024f9a90501SMatteo Croce 			 * reboot_cpu is s[mp]#### with #### being the processor
1025f9a90501SMatteo Croce 			 * to be used for rebooting. Skip 's' or 'smp' prefix.
1026f9a90501SMatteo Croce 			 */
1027f9a90501SMatteo Croce 			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
1028f9a90501SMatteo Croce 
1029f9a90501SMatteo Croce 			if (isdigit(str[0])) {
1030f9a90501SMatteo Croce 				int cpu = simple_strtoul(str, NULL, 0);
1031f9a90501SMatteo Croce 
1032f9a90501SMatteo Croce 				if (cpu >= num_possible_cpus()) {
1033df5b0ab3SMatteo Croce 					pr_err("Ignoring the CPU number in reboot= option. "
1034df5b0ab3SMatteo Croce 					"CPU %d exceeds possible cpu number %d\n",
1035f9a90501SMatteo Croce 					cpu, num_possible_cpus());
1036df5b0ab3SMatteo Croce 					break;
1037df5b0ab3SMatteo Croce 				}
1038f9a90501SMatteo Croce 				reboot_cpu = cpu;
1039f9a90501SMatteo Croce 			} else
1040f9a90501SMatteo Croce 				*mode = REBOOT_SOFT;
10411b3a5d02SRobin Holt 			break;
10428b92c4ffSMatteo Croce 
10431b3a5d02SRobin Holt 		case 'g':
1044b287a25aSAaro Koskinen 			*mode = REBOOT_GPIO;
10451b3a5d02SRobin Holt 			break;
10461b3a5d02SRobin Holt 
10471b3a5d02SRobin Holt 		case 'b':
10481b3a5d02SRobin Holt 		case 'a':
10491b3a5d02SRobin Holt 		case 'k':
10501b3a5d02SRobin Holt 		case 't':
10511b3a5d02SRobin Holt 		case 'e':
10521b3a5d02SRobin Holt 		case 'p':
10531b3a5d02SRobin Holt 			reboot_type = *str;
10541b3a5d02SRobin Holt 			break;
10551b3a5d02SRobin Holt 
10561b3a5d02SRobin Holt 		case 'f':
10571b3a5d02SRobin Holt 			reboot_force = 1;
10581b3a5d02SRobin Holt 			break;
10591b3a5d02SRobin Holt 		}
10601b3a5d02SRobin Holt 
10611b3a5d02SRobin Holt 		str = strchr(str, ',');
10621b3a5d02SRobin Holt 		if (str)
10631b3a5d02SRobin Holt 			str++;
10641b3a5d02SRobin Holt 		else
10651b3a5d02SRobin Holt 			break;
10661b3a5d02SRobin Holt 	}
10671b3a5d02SRobin Holt 	return 1;
10681b3a5d02SRobin Holt }
10691b3a5d02SRobin Holt __setup("reboot=", reboot_setup);
10702c622ed0SMatteo Croce 
10712c622ed0SMatteo Croce #ifdef CONFIG_SYSFS
10722c622ed0SMatteo Croce 
10732c622ed0SMatteo Croce #define REBOOT_COLD_STR		"cold"
10742c622ed0SMatteo Croce #define REBOOT_WARM_STR		"warm"
10752c622ed0SMatteo Croce #define REBOOT_HARD_STR		"hard"
10762c622ed0SMatteo Croce #define REBOOT_SOFT_STR		"soft"
10772c622ed0SMatteo Croce #define REBOOT_GPIO_STR		"gpio"
10782c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR	"undefined"
10792c622ed0SMatteo Croce 
10802c622ed0SMatteo Croce #define BOOT_TRIPLE_STR		"triple"
10812c622ed0SMatteo Croce #define BOOT_KBD_STR		"kbd"
10822c622ed0SMatteo Croce #define BOOT_BIOS_STR		"bios"
10832c622ed0SMatteo Croce #define BOOT_ACPI_STR		"acpi"
10842c622ed0SMatteo Croce #define BOOT_EFI_STR		"efi"
10850c5c0179SMatteo Croce #define BOOT_PCI_STR		"pci"
10862c622ed0SMatteo Croce 
mode_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)10872c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
10882c622ed0SMatteo Croce {
10892c622ed0SMatteo Croce 	const char *val;
10902c622ed0SMatteo Croce 
10912c622ed0SMatteo Croce 	switch (reboot_mode) {
10922c622ed0SMatteo Croce 	case REBOOT_COLD:
10932c622ed0SMatteo Croce 		val = REBOOT_COLD_STR;
10942c622ed0SMatteo Croce 		break;
10952c622ed0SMatteo Croce 	case REBOOT_WARM:
10962c622ed0SMatteo Croce 		val = REBOOT_WARM_STR;
10972c622ed0SMatteo Croce 		break;
10982c622ed0SMatteo Croce 	case REBOOT_HARD:
10992c622ed0SMatteo Croce 		val = REBOOT_HARD_STR;
11002c622ed0SMatteo Croce 		break;
11012c622ed0SMatteo Croce 	case REBOOT_SOFT:
11022c622ed0SMatteo Croce 		val = REBOOT_SOFT_STR;
11032c622ed0SMatteo Croce 		break;
11042c622ed0SMatteo Croce 	case REBOOT_GPIO:
11052c622ed0SMatteo Croce 		val = REBOOT_GPIO_STR;
11062c622ed0SMatteo Croce 		break;
11072c622ed0SMatteo Croce 	default:
11082c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
11092c622ed0SMatteo Croce 	}
11102c622ed0SMatteo Croce 
11112c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
11122c622ed0SMatteo Croce }
mode_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)11132c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
11142c622ed0SMatteo Croce 			  const char *buf, size_t count)
11152c622ed0SMatteo Croce {
11162c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
11172c622ed0SMatteo Croce 		return -EPERM;
11182c622ed0SMatteo Croce 
11192c622ed0SMatteo Croce 	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
11202c622ed0SMatteo Croce 		reboot_mode = REBOOT_COLD;
11212c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
11222c622ed0SMatteo Croce 		reboot_mode = REBOOT_WARM;
11232c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
11242c622ed0SMatteo Croce 		reboot_mode = REBOOT_HARD;
11252c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
11262c622ed0SMatteo Croce 		reboot_mode = REBOOT_SOFT;
11272c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
11282c622ed0SMatteo Croce 		reboot_mode = REBOOT_GPIO;
11292c622ed0SMatteo Croce 	else
11302c622ed0SMatteo Croce 		return -EINVAL;
11312c622ed0SMatteo Croce 
11321a9d079fSMatteo Croce 	reboot_default = 0;
11331a9d079fSMatteo Croce 
11342c622ed0SMatteo Croce 	return count;
11352c622ed0SMatteo Croce }
11362c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
11372c622ed0SMatteo Croce 
113840247e55SMatteo Croce #ifdef CONFIG_X86
force_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)113940247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
114040247e55SMatteo Croce {
114140247e55SMatteo Croce 	return sprintf(buf, "%d\n", reboot_force);
114240247e55SMatteo Croce }
force_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)114340247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
114440247e55SMatteo Croce 			  const char *buf, size_t count)
114540247e55SMatteo Croce {
114640247e55SMatteo Croce 	bool res;
114740247e55SMatteo Croce 
114840247e55SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
114940247e55SMatteo Croce 		return -EPERM;
115040247e55SMatteo Croce 
115140247e55SMatteo Croce 	if (kstrtobool(buf, &res))
115240247e55SMatteo Croce 		return -EINVAL;
115340247e55SMatteo Croce 
115440247e55SMatteo Croce 	reboot_default = 0;
115540247e55SMatteo Croce 	reboot_force = res;
115640247e55SMatteo Croce 
115740247e55SMatteo Croce 	return count;
115840247e55SMatteo Croce }
115940247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
116040247e55SMatteo Croce 
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)11612c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
11622c622ed0SMatteo Croce {
11632c622ed0SMatteo Croce 	const char *val;
11642c622ed0SMatteo Croce 
11652c622ed0SMatteo Croce 	switch (reboot_type) {
11662c622ed0SMatteo Croce 	case BOOT_TRIPLE:
11672c622ed0SMatteo Croce 		val = BOOT_TRIPLE_STR;
11682c622ed0SMatteo Croce 		break;
11692c622ed0SMatteo Croce 	case BOOT_KBD:
11702c622ed0SMatteo Croce 		val = BOOT_KBD_STR;
11712c622ed0SMatteo Croce 		break;
11722c622ed0SMatteo Croce 	case BOOT_BIOS:
11732c622ed0SMatteo Croce 		val = BOOT_BIOS_STR;
11742c622ed0SMatteo Croce 		break;
11752c622ed0SMatteo Croce 	case BOOT_ACPI:
11762c622ed0SMatteo Croce 		val = BOOT_ACPI_STR;
11772c622ed0SMatteo Croce 		break;
11782c622ed0SMatteo Croce 	case BOOT_EFI:
11792c622ed0SMatteo Croce 		val = BOOT_EFI_STR;
11802c622ed0SMatteo Croce 		break;
11812c622ed0SMatteo Croce 	case BOOT_CF9_FORCE:
11820c5c0179SMatteo Croce 		val = BOOT_PCI_STR;
11832c622ed0SMatteo Croce 		break;
11842c622ed0SMatteo Croce 	default:
11852c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
11862c622ed0SMatteo Croce 	}
11872c622ed0SMatteo Croce 
11882c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
11892c622ed0SMatteo Croce }
type_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)11902c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
11912c622ed0SMatteo Croce 			  const char *buf, size_t count)
11922c622ed0SMatteo Croce {
11932c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
11942c622ed0SMatteo Croce 		return -EPERM;
11952c622ed0SMatteo Croce 
11962c622ed0SMatteo Croce 	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
11972c622ed0SMatteo Croce 		reboot_type = BOOT_TRIPLE;
11982c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
11992c622ed0SMatteo Croce 		reboot_type = BOOT_KBD;
12002c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
12012c622ed0SMatteo Croce 		reboot_type = BOOT_BIOS;
12022c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
12032c622ed0SMatteo Croce 		reboot_type = BOOT_ACPI;
12042c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
12052c622ed0SMatteo Croce 		reboot_type = BOOT_EFI;
12060c5c0179SMatteo Croce 	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
12072c622ed0SMatteo Croce 		reboot_type = BOOT_CF9_FORCE;
12082c622ed0SMatteo Croce 	else
12092c622ed0SMatteo Croce 		return -EINVAL;
12102c622ed0SMatteo Croce 
12111a9d079fSMatteo Croce 	reboot_default = 0;
12121a9d079fSMatteo Croce 
12132c622ed0SMatteo Croce 	return count;
12142c622ed0SMatteo Croce }
12152c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
121640247e55SMatteo Croce #endif
12172c622ed0SMatteo Croce 
121840247e55SMatteo Croce #ifdef CONFIG_SMP
cpu_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)12192c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
12202c622ed0SMatteo Croce {
12212c622ed0SMatteo Croce 	return sprintf(buf, "%d\n", reboot_cpu);
12222c622ed0SMatteo Croce }
cpu_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)12232c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
12242c622ed0SMatteo Croce 			  const char *buf, size_t count)
12252c622ed0SMatteo Croce {
12262c622ed0SMatteo Croce 	unsigned int cpunum;
12272c622ed0SMatteo Croce 	int rc;
12282c622ed0SMatteo Croce 
12292c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
12302c622ed0SMatteo Croce 		return -EPERM;
12312c622ed0SMatteo Croce 
12322c622ed0SMatteo Croce 	rc = kstrtouint(buf, 0, &cpunum);
12332c622ed0SMatteo Croce 
12342c622ed0SMatteo Croce 	if (rc)
12352c622ed0SMatteo Croce 		return rc;
12362c622ed0SMatteo Croce 
12372c622ed0SMatteo Croce 	if (cpunum >= num_possible_cpus())
12382c622ed0SMatteo Croce 		return -ERANGE;
12392c622ed0SMatteo Croce 
12401a9d079fSMatteo Croce 	reboot_default = 0;
12412c622ed0SMatteo Croce 	reboot_cpu = cpunum;
12422c622ed0SMatteo Croce 
12432c622ed0SMatteo Croce 	return count;
12442c622ed0SMatteo Croce }
12452c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
124640247e55SMatteo Croce #endif
12472c622ed0SMatteo Croce 
12482c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = {
12492c622ed0SMatteo Croce 	&reboot_mode_attr.attr,
125040247e55SMatteo Croce #ifdef CONFIG_X86
12512c622ed0SMatteo Croce 	&reboot_force_attr.attr,
125240247e55SMatteo Croce 	&reboot_type_attr.attr,
125340247e55SMatteo Croce #endif
125440247e55SMatteo Croce #ifdef CONFIG_SMP
125540247e55SMatteo Croce 	&reboot_cpu_attr.attr,
125640247e55SMatteo Croce #endif
12572c622ed0SMatteo Croce 	NULL,
12582c622ed0SMatteo Croce };
12592c622ed0SMatteo Croce 
1260764aaf44SYueHaibing #ifdef CONFIG_SYSCTL
1261764aaf44SYueHaibing static struct ctl_table kern_reboot_table[] = {
1262764aaf44SYueHaibing 	{
1263764aaf44SYueHaibing 		.procname       = "poweroff_cmd",
1264764aaf44SYueHaibing 		.data           = &poweroff_cmd,
1265764aaf44SYueHaibing 		.maxlen         = POWEROFF_CMD_PATH_LEN,
1266764aaf44SYueHaibing 		.mode           = 0644,
1267764aaf44SYueHaibing 		.proc_handler   = proc_dostring,
1268764aaf44SYueHaibing 	},
1269764aaf44SYueHaibing 	{
1270764aaf44SYueHaibing 		.procname       = "ctrl-alt-del",
1271764aaf44SYueHaibing 		.data           = &C_A_D,
1272764aaf44SYueHaibing 		.maxlen         = sizeof(int),
1273764aaf44SYueHaibing 		.mode           = 0644,
1274764aaf44SYueHaibing 		.proc_handler   = proc_dointvec,
1275764aaf44SYueHaibing 	},
1276764aaf44SYueHaibing 	{ }
1277764aaf44SYueHaibing };
1278764aaf44SYueHaibing 
kernel_reboot_sysctls_init(void)1279764aaf44SYueHaibing static void __init kernel_reboot_sysctls_init(void)
1280764aaf44SYueHaibing {
1281764aaf44SYueHaibing 	register_sysctl_init("kernel", kern_reboot_table);
1282764aaf44SYueHaibing }
1283764aaf44SYueHaibing #else
1284764aaf44SYueHaibing #define kernel_reboot_sysctls_init() do { } while (0)
1285764aaf44SYueHaibing #endif /* CONFIG_SYSCTL */
1286764aaf44SYueHaibing 
12872c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = {
12882c622ed0SMatteo Croce 	.attrs = reboot_attrs,
12892c622ed0SMatteo Croce };
12902c622ed0SMatteo Croce 
reboot_ksysfs_init(void)12912c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void)
12922c622ed0SMatteo Croce {
12932c622ed0SMatteo Croce 	struct kobject *reboot_kobj;
12942c622ed0SMatteo Croce 	int ret;
12952c622ed0SMatteo Croce 
12962c622ed0SMatteo Croce 	reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
12972c622ed0SMatteo Croce 	if (!reboot_kobj)
12982c622ed0SMatteo Croce 		return -ENOMEM;
12992c622ed0SMatteo Croce 
13002c622ed0SMatteo Croce 	ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
13012c622ed0SMatteo Croce 	if (ret) {
13022c622ed0SMatteo Croce 		kobject_put(reboot_kobj);
13032c622ed0SMatteo Croce 		return ret;
13042c622ed0SMatteo Croce 	}
13052c622ed0SMatteo Croce 
130606d17766Stangmeng 	kernel_reboot_sysctls_init();
130706d17766Stangmeng 
13082c622ed0SMatteo Croce 	return 0;
13092c622ed0SMatteo Croce }
13102c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init);
13112c622ed0SMatteo Croce 
13122c622ed0SMatteo Croce #endif
1313