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