xref: /linux/kernel/power/suspend.c (revision c54ea4918c2b7722d7242ea53271356501988a9b)
1 /*
2  * kernel/power/suspend.c - Suspend to RAM and standby functionality.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
7  *
8  * This file is released under the GPLv2.
9  */
10 
11 #include <linux/string.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/console.h>
16 #include <linux/cpu.h>
17 #include <linux/syscalls.h>
18 #include <linux/gfp.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/suspend.h>
25 #include <linux/syscore_ops.h>
26 #include <trace/events/power.h>
27 
28 #include "power.h"
29 
30 const char *const pm_states[PM_SUSPEND_MAX] = {
31 	[PM_SUSPEND_STANDBY]	= "standby",
32 	[PM_SUSPEND_MEM]	= "mem",
33 };
34 
35 static const struct platform_suspend_ops *suspend_ops;
36 
37 /**
38  *	suspend_set_ops - Set the global suspend method table.
39  *	@ops:	Pointer to ops structure.
40  */
41 void suspend_set_ops(const struct platform_suspend_ops *ops)
42 {
43 	mutex_lock(&pm_mutex);
44 	suspend_ops = ops;
45 	mutex_unlock(&pm_mutex);
46 }
47 
48 bool valid_state(suspend_state_t state)
49 {
50 	/*
51 	 * All states need lowlevel support and need to be valid to the lowlevel
52 	 * implementation, no valid callback implies that none are valid.
53 	 */
54 	return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
55 }
56 
57 /**
58  * suspend_valid_only_mem - generic memory-only valid callback
59  *
60  * Platform drivers that implement mem suspend only and only need
61  * to check for that in their .valid callback can use this instead
62  * of rolling their own .valid callback.
63  */
64 int suspend_valid_only_mem(suspend_state_t state)
65 {
66 	return state == PM_SUSPEND_MEM;
67 }
68 
69 static int suspend_test(int level)
70 {
71 #ifdef CONFIG_PM_DEBUG
72 	if (pm_test_level == level) {
73 		printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
74 		mdelay(5000);
75 		return 1;
76 	}
77 #endif /* !CONFIG_PM_DEBUG */
78 	return 0;
79 }
80 
81 /**
82  *	suspend_prepare - Do prep work before entering low-power state.
83  *
84  *	This is common code that is called for each state that we're entering.
85  *	Run suspend notifiers, allocate a console and stop all processes.
86  */
87 static int suspend_prepare(void)
88 {
89 	int error;
90 
91 	if (!suspend_ops || !suspend_ops->enter)
92 		return -EPERM;
93 
94 	pm_prepare_console();
95 
96 	error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
97 	if (error)
98 		goto Finish;
99 
100 	error = usermodehelper_disable();
101 	if (error)
102 		goto Finish;
103 
104 	error = suspend_freeze_processes();
105 	if (!error)
106 		return 0;
107 
108 	suspend_thaw_processes();
109 	usermodehelper_enable();
110  Finish:
111 	pm_notifier_call_chain(PM_POST_SUSPEND);
112 	pm_restore_console();
113 	return error;
114 }
115 
116 /* default implementation */
117 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
118 {
119 	local_irq_disable();
120 }
121 
122 /* default implementation */
123 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
124 {
125 	local_irq_enable();
126 }
127 
128 /**
129  *	suspend_enter - enter the desired system sleep state.
130  *	@state:		state to enter
131  *
132  *	This function should be called after devices have been suspended.
133  */
134 static int suspend_enter(suspend_state_t state)
135 {
136 	int error;
137 
138 	if (suspend_ops->prepare) {
139 		error = suspend_ops->prepare();
140 		if (error)
141 			goto Platform_finish;
142 	}
143 
144 	error = dpm_suspend_noirq(PMSG_SUSPEND);
145 	if (error) {
146 		printk(KERN_ERR "PM: Some devices failed to power down\n");
147 		goto Platform_finish;
148 	}
149 
150 	if (suspend_ops->prepare_late) {
151 		error = suspend_ops->prepare_late();
152 		if (error)
153 			goto Platform_wake;
154 	}
155 
156 	if (suspend_test(TEST_PLATFORM))
157 		goto Platform_wake;
158 
159 	error = disable_nonboot_cpus();
160 	if (error || suspend_test(TEST_CPUS))
161 		goto Enable_cpus;
162 
163 	arch_suspend_disable_irqs();
164 	BUG_ON(!irqs_disabled());
165 
166 	error = sysdev_suspend(PMSG_SUSPEND);
167 	if (!error)
168 		error = syscore_suspend();
169 	if (!error) {
170 		if (!(suspend_test(TEST_CORE) || pm_wakeup_pending())) {
171 			error = suspend_ops->enter(state);
172 			events_check_enabled = false;
173 		}
174 		syscore_resume();
175 		sysdev_resume();
176 	}
177 
178 	arch_suspend_enable_irqs();
179 	BUG_ON(irqs_disabled());
180 
181  Enable_cpus:
182 	enable_nonboot_cpus();
183 
184  Platform_wake:
185 	if (suspend_ops->wake)
186 		suspend_ops->wake();
187 
188 	dpm_resume_noirq(PMSG_RESUME);
189 
190  Platform_finish:
191 	if (suspend_ops->finish)
192 		suspend_ops->finish();
193 
194 	return error;
195 }
196 
197 /**
198  *	suspend_devices_and_enter - suspend devices and enter the desired system
199  *				    sleep state.
200  *	@state:		  state to enter
201  */
202 int suspend_devices_and_enter(suspend_state_t state)
203 {
204 	int error;
205 
206 	if (!suspend_ops)
207 		return -ENOSYS;
208 
209 	trace_machine_suspend(state);
210 	if (suspend_ops->begin) {
211 		error = suspend_ops->begin(state);
212 		if (error)
213 			goto Close;
214 	}
215 	suspend_console();
216 	pm_restrict_gfp_mask();
217 	suspend_test_start();
218 	error = dpm_suspend_start(PMSG_SUSPEND);
219 	if (error) {
220 		printk(KERN_ERR "PM: Some devices failed to suspend\n");
221 		goto Recover_platform;
222 	}
223 	suspend_test_finish("suspend devices");
224 	if (suspend_test(TEST_DEVICES))
225 		goto Recover_platform;
226 
227 	suspend_enter(state);
228 
229  Resume_devices:
230 	suspend_test_start();
231 	dpm_resume_end(PMSG_RESUME);
232 	suspend_test_finish("resume devices");
233 	pm_restore_gfp_mask();
234 	resume_console();
235  Close:
236 	if (suspend_ops->end)
237 		suspend_ops->end();
238 	trace_machine_suspend(PWR_EVENT_EXIT);
239 	return error;
240 
241  Recover_platform:
242 	if (suspend_ops->recover)
243 		suspend_ops->recover();
244 	goto Resume_devices;
245 }
246 
247 /**
248  *	suspend_finish - Do final work before exiting suspend sequence.
249  *
250  *	Call platform code to clean up, restart processes, and free the
251  *	console that we've allocated. This is not called for suspend-to-disk.
252  */
253 static void suspend_finish(void)
254 {
255 	suspend_thaw_processes();
256 	usermodehelper_enable();
257 	pm_notifier_call_chain(PM_POST_SUSPEND);
258 	pm_restore_console();
259 }
260 
261 /**
262  *	enter_state - Do common work of entering low-power state.
263  *	@state:		pm_state structure for state we're entering.
264  *
265  *	Make sure we're the only ones trying to enter a sleep state. Fail
266  *	if someone has beat us to it, since we don't want anything weird to
267  *	happen when we wake up.
268  *	Then, do the setup for suspend, enter the state, and cleaup (after
269  *	we've woken up).
270  */
271 int enter_state(suspend_state_t state)
272 {
273 	int error;
274 
275 	if (!valid_state(state))
276 		return -ENODEV;
277 
278 	if (!mutex_trylock(&pm_mutex))
279 		return -EBUSY;
280 
281 	printk(KERN_INFO "PM: Syncing filesystems ... ");
282 	sys_sync();
283 	printk("done.\n");
284 
285 	pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
286 	error = suspend_prepare();
287 	if (error)
288 		goto Unlock;
289 
290 	if (suspend_test(TEST_FREEZER))
291 		goto Finish;
292 
293 	pr_debug("PM: Entering %s sleep\n", pm_states[state]);
294 	error = suspend_devices_and_enter(state);
295 
296  Finish:
297 	pr_debug("PM: Finishing wakeup.\n");
298 	suspend_finish();
299  Unlock:
300 	mutex_unlock(&pm_mutex);
301 	return error;
302 }
303 
304 /**
305  *	pm_suspend - Externally visible function for suspending system.
306  *	@state:		Enumerated value of state to enter.
307  *
308  *	Determine whether or not value is within range, get state
309  *	structure, and enter (above).
310  */
311 int pm_suspend(suspend_state_t state)
312 {
313 	if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
314 		return enter_state(state);
315 	return -EINVAL;
316 }
317 EXPORT_SYMBOL(pm_suspend);
318