• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cpuidle.h>
18 #include <linux/syscalls.h>
19 #include <linux/gfp.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/suspend.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/ftrace.h>
29 #include <linux/rtc.h>
30 #include <trace/events/power.h>
31 #include <linux/compiler.h>
32 #include <linux/wakeup_reason.h>
33 
34 #include "power.h"
35 
36 const char *pm_labels[] = { "mem", "standby", "freeze", NULL };
37 const char *pm_states[PM_SUSPEND_MAX];
38 
39 static const struct platform_suspend_ops *suspend_ops;
40 static const struct platform_freeze_ops *freeze_ops;
41 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
42 
43 enum freeze_state __read_mostly suspend_freeze_state;
44 static DEFINE_SPINLOCK(suspend_freeze_lock);
45 
freeze_set_ops(const struct platform_freeze_ops * ops)46 void freeze_set_ops(const struct platform_freeze_ops *ops)
47 {
48 	lock_system_sleep();
49 	freeze_ops = ops;
50 	unlock_system_sleep();
51 }
52 
freeze_begin(void)53 static void freeze_begin(void)
54 {
55 	suspend_freeze_state = FREEZE_STATE_NONE;
56 }
57 
freeze_enter(void)58 static void freeze_enter(void)
59 {
60 	spin_lock_irq(&suspend_freeze_lock);
61 	if (pm_wakeup_pending())
62 		goto out;
63 
64 	suspend_freeze_state = FREEZE_STATE_ENTER;
65 	spin_unlock_irq(&suspend_freeze_lock);
66 
67 	get_online_cpus();
68 	cpuidle_resume();
69 
70 	/* Push all the CPUs into the idle loop. */
71 	wake_up_all_idle_cpus();
72 	pr_debug("PM: suspend-to-idle\n");
73 	/* Make the current CPU wait so it can enter the idle loop too. */
74 	wait_event(suspend_freeze_wait_head,
75 		   suspend_freeze_state == FREEZE_STATE_WAKE);
76 	pr_debug("PM: resume from suspend-to-idle\n");
77 
78 	cpuidle_pause();
79 	put_online_cpus();
80 
81 	spin_lock_irq(&suspend_freeze_lock);
82 
83  out:
84 	suspend_freeze_state = FREEZE_STATE_NONE;
85 	spin_unlock_irq(&suspend_freeze_lock);
86 }
87 
freeze_wake(void)88 void freeze_wake(void)
89 {
90 	unsigned long flags;
91 
92 	spin_lock_irqsave(&suspend_freeze_lock, flags);
93 	if (suspend_freeze_state > FREEZE_STATE_NONE) {
94 		suspend_freeze_state = FREEZE_STATE_WAKE;
95 		wake_up(&suspend_freeze_wait_head);
96 	}
97 	spin_unlock_irqrestore(&suspend_freeze_lock, flags);
98 }
99 EXPORT_SYMBOL_GPL(freeze_wake);
100 
valid_state(suspend_state_t state)101 static bool valid_state(suspend_state_t state)
102 {
103 	/*
104 	 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
105 	 * support and need to be valid to the low level
106 	 * implementation, no valid callback implies that none are valid.
107 	 */
108 	return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
109 }
110 
111 /*
112  * If this is set, the "mem" label always corresponds to the deepest sleep state
113  * available, the "standby" label corresponds to the second deepest sleep state
114  * available (if any), and the "freeze" label corresponds to the remaining
115  * available sleep state (if there is one).
116  */
117 static bool relative_states;
118 
sleep_states_setup(char * str)119 static int __init sleep_states_setup(char *str)
120 {
121 	relative_states = !strncmp(str, "1", 1);
122 	pm_states[PM_SUSPEND_FREEZE] = pm_labels[relative_states ? 0 : 2];
123 	return 1;
124 }
125 
126 __setup("relative_sleep_states=", sleep_states_setup);
127 
128 /**
129  * suspend_set_ops - Set the global suspend method table.
130  * @ops: Suspend operations to use.
131  */
suspend_set_ops(const struct platform_suspend_ops * ops)132 void suspend_set_ops(const struct platform_suspend_ops *ops)
133 {
134 	suspend_state_t i;
135 	int j = 0;
136 
137 	lock_system_sleep();
138 
139 	suspend_ops = ops;
140 	for (i = PM_SUSPEND_MEM; i >= PM_SUSPEND_STANDBY; i--)
141 		if (valid_state(i)) {
142 			pm_states[i] = pm_labels[j++];
143 		} else if (!relative_states) {
144 			pm_states[i] = NULL;
145 			j++;
146 		}
147 
148 	pm_states[PM_SUSPEND_FREEZE] = pm_labels[j];
149 
150 	unlock_system_sleep();
151 }
152 EXPORT_SYMBOL_GPL(suspend_set_ops);
153 
154 /**
155  * suspend_valid_only_mem - Generic memory-only valid callback.
156  *
157  * Platform drivers that implement mem suspend only and only need to check for
158  * that in their .valid() callback can use this instead of rolling their own
159  * .valid() callback.
160  */
suspend_valid_only_mem(suspend_state_t state)161 int suspend_valid_only_mem(suspend_state_t state)
162 {
163 	return state == PM_SUSPEND_MEM;
164 }
165 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
166 
sleep_state_supported(suspend_state_t state)167 static bool sleep_state_supported(suspend_state_t state)
168 {
169 	return state == PM_SUSPEND_FREEZE || (suspend_ops && suspend_ops->enter);
170 }
171 
platform_suspend_prepare(suspend_state_t state)172 static int platform_suspend_prepare(suspend_state_t state)
173 {
174 	return state != PM_SUSPEND_FREEZE && suspend_ops->prepare ?
175 		suspend_ops->prepare() : 0;
176 }
177 
platform_suspend_prepare_late(suspend_state_t state)178 static int platform_suspend_prepare_late(suspend_state_t state)
179 {
180 	return state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->prepare ?
181 		freeze_ops->prepare() : 0;
182 }
183 
platform_suspend_prepare_noirq(suspend_state_t state)184 static int platform_suspend_prepare_noirq(suspend_state_t state)
185 {
186 	return state != PM_SUSPEND_FREEZE && suspend_ops->prepare_late ?
187 		suspend_ops->prepare_late() : 0;
188 }
189 
platform_resume_noirq(suspend_state_t state)190 static void platform_resume_noirq(suspend_state_t state)
191 {
192 	if (state != PM_SUSPEND_FREEZE && suspend_ops->wake)
193 		suspend_ops->wake();
194 }
195 
platform_resume_early(suspend_state_t state)196 static void platform_resume_early(suspend_state_t state)
197 {
198 	if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->restore)
199 		freeze_ops->restore();
200 }
201 
platform_resume_finish(suspend_state_t state)202 static void platform_resume_finish(suspend_state_t state)
203 {
204 	if (state != PM_SUSPEND_FREEZE && suspend_ops->finish)
205 		suspend_ops->finish();
206 }
207 
platform_suspend_begin(suspend_state_t state)208 static int platform_suspend_begin(suspend_state_t state)
209 {
210 	if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->begin)
211 		return freeze_ops->begin();
212 	else if (suspend_ops->begin)
213 		return suspend_ops->begin(state);
214 	else
215 		return 0;
216 }
217 
platform_resume_end(suspend_state_t state)218 static void platform_resume_end(suspend_state_t state)
219 {
220 	if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->end)
221 		freeze_ops->end();
222 	else if (suspend_ops->end)
223 		suspend_ops->end();
224 }
225 
platform_recover(suspend_state_t state)226 static void platform_recover(suspend_state_t state)
227 {
228 	if (state != PM_SUSPEND_FREEZE && suspend_ops->recover)
229 		suspend_ops->recover();
230 }
231 
platform_suspend_again(suspend_state_t state)232 static bool platform_suspend_again(suspend_state_t state)
233 {
234 	return state != PM_SUSPEND_FREEZE && suspend_ops->suspend_again ?
235 		suspend_ops->suspend_again() : false;
236 }
237 
suspend_test(int level)238 static int suspend_test(int level)
239 {
240 #ifdef CONFIG_PM_DEBUG
241 	if (pm_test_level == level) {
242 		printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
243 		mdelay(5000);
244 		return 1;
245 	}
246 #endif /* !CONFIG_PM_DEBUG */
247 	return 0;
248 }
249 
250 /**
251  * suspend_prepare - Prepare for entering system sleep state.
252  *
253  * Common code run for every system sleep state that can be entered (except for
254  * hibernation).  Run suspend notifiers, allocate the "suspend" console and
255  * freeze processes.
256  */
suspend_prepare(suspend_state_t state)257 static int suspend_prepare(suspend_state_t state)
258 {
259 	int error, nr_calls = 0;
260 
261 	if (!sleep_state_supported(state))
262 		return -EPERM;
263 
264 	pm_prepare_console();
265 
266 	error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
267 	if (error) {
268 		nr_calls--;
269 		goto Finish;
270 	}
271 
272 	trace_suspend_resume(TPS("freeze_processes"), 0, true);
273 	error = suspend_freeze_processes();
274 	trace_suspend_resume(TPS("freeze_processes"), 0, false);
275 	if (!error)
276 		return 0;
277 
278 	suspend_stats.failed_freeze++;
279 	dpm_save_failed_step(SUSPEND_FREEZE);
280  Finish:
281 	__pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
282 	pm_restore_console();
283 	return error;
284 }
285 
286 /* default implementation */
arch_suspend_disable_irqs(void)287 void __weak arch_suspend_disable_irqs(void)
288 {
289 	local_irq_disable();
290 }
291 
292 /* default implementation */
arch_suspend_enable_irqs(void)293 void __weak arch_suspend_enable_irqs(void)
294 {
295 	local_irq_enable();
296 }
297 
298 /**
299  * suspend_enter - Make the system enter the given sleep state.
300  * @state: System sleep state to enter.
301  * @wakeup: Returns information that the sleep state should not be re-entered.
302  *
303  * This function should be called after devices have been suspended.
304  */
suspend_enter(suspend_state_t state,bool * wakeup)305 static int suspend_enter(suspend_state_t state, bool *wakeup)
306 {
307 	char suspend_abort[MAX_SUSPEND_ABORT_LEN];
308 	int error, last_dev;
309 
310 	error = platform_suspend_prepare(state);
311 	if (error)
312 		goto Platform_finish;
313 
314 	error = dpm_suspend_late(PMSG_SUSPEND);
315 	if (error) {
316 		last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
317 		last_dev %= REC_FAILED_NUM;
318 		printk(KERN_ERR "PM: late suspend of devices failed\n");
319 		log_suspend_abort_reason("%s device failed to power down",
320 			suspend_stats.failed_devs[last_dev]);
321 		goto Platform_finish;
322 	}
323 	error = platform_suspend_prepare_late(state);
324 	if (error)
325 		goto Devices_early_resume;
326 
327 	error = dpm_suspend_noirq(PMSG_SUSPEND);
328 	if (error) {
329 		last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
330 		last_dev %= REC_FAILED_NUM;
331 		printk(KERN_ERR "PM: noirq suspend of devices failed\n");
332 		log_suspend_abort_reason("noirq suspend of %s device failed",
333 			suspend_stats.failed_devs[last_dev]);
334 		goto Platform_early_resume;
335 	}
336 	error = platform_suspend_prepare_noirq(state);
337 	if (error)
338 		goto Platform_wake;
339 
340 	if (suspend_test(TEST_PLATFORM))
341 		goto Platform_wake;
342 
343 	/*
344 	 * PM_SUSPEND_FREEZE equals
345 	 * frozen processes + suspended devices + idle processors.
346 	 * Thus we should invoke freeze_enter() soon after
347 	 * all the devices are suspended.
348 	 */
349 	if (state == PM_SUSPEND_FREEZE) {
350 		trace_suspend_resume(TPS("machine_suspend"), state, true);
351 		freeze_enter();
352 		trace_suspend_resume(TPS("machine_suspend"), state, false);
353 		goto Platform_wake;
354 	}
355 
356 	error = disable_nonboot_cpus();
357 	if (error || suspend_test(TEST_CPUS)) {
358 		log_suspend_abort_reason("Disabling non-boot cpus failed");
359 		goto Enable_cpus;
360 	}
361 
362 	arch_suspend_disable_irqs();
363 	BUG_ON(!irqs_disabled());
364 
365 	error = syscore_suspend();
366 	if (!error) {
367 		*wakeup = pm_wakeup_pending();
368 		if (!(suspend_test(TEST_CORE) || *wakeup)) {
369 			trace_suspend_resume(TPS("machine_suspend"),
370 				state, true);
371 			error = suspend_ops->enter(state);
372 			trace_suspend_resume(TPS("machine_suspend"),
373 				state, false);
374 			events_check_enabled = false;
375 		} else if (*wakeup) {
376 			pm_get_active_wakeup_sources(suspend_abort,
377 				MAX_SUSPEND_ABORT_LEN);
378 			log_suspend_abort_reason(suspend_abort);
379 			error = -EBUSY;
380 		}
381 		syscore_resume();
382 	}
383 
384 	arch_suspend_enable_irqs();
385 	BUG_ON(irqs_disabled());
386 
387  Enable_cpus:
388 	enable_nonboot_cpus();
389 
390  Platform_wake:
391 	platform_resume_noirq(state);
392 	dpm_resume_noirq(PMSG_RESUME);
393 
394  Platform_early_resume:
395 	platform_resume_early(state);
396 
397  Devices_early_resume:
398 	dpm_resume_early(PMSG_RESUME);
399 
400  Platform_finish:
401 	platform_resume_finish(state);
402 	return error;
403 }
404 
405 /**
406  * suspend_devices_and_enter - Suspend devices and enter system sleep state.
407  * @state: System sleep state to enter.
408  */
suspend_devices_and_enter(suspend_state_t state)409 int suspend_devices_and_enter(suspend_state_t state)
410 {
411 	int error;
412 	bool wakeup = false;
413 
414 	if (!sleep_state_supported(state))
415 		return -ENOSYS;
416 
417 	error = platform_suspend_begin(state);
418 	if (error)
419 		goto Close;
420 
421 	suspend_console();
422 	suspend_test_start();
423 	error = dpm_suspend_start(PMSG_SUSPEND);
424 	if (error) {
425 		pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
426 		log_suspend_abort_reason("Some devices failed to suspend, or early wake event detected");
427 		goto Recover_platform;
428 	}
429 	suspend_test_finish("suspend devices");
430 	if (suspend_test(TEST_DEVICES))
431 		goto Recover_platform;
432 
433 	do {
434 		error = suspend_enter(state, &wakeup);
435 	} while (!error && !wakeup && platform_suspend_again(state));
436 
437  Resume_devices:
438 	suspend_test_start();
439 	dpm_resume_end(PMSG_RESUME);
440 	suspend_test_finish("resume devices");
441 	trace_suspend_resume(TPS("resume_console"), state, true);
442 	resume_console();
443 	trace_suspend_resume(TPS("resume_console"), state, false);
444 
445  Close:
446 	platform_resume_end(state);
447 	return error;
448 
449  Recover_platform:
450 	platform_recover(state);
451 	goto Resume_devices;
452 }
453 
454 /**
455  * suspend_finish - Clean up before finishing the suspend sequence.
456  *
457  * Call platform code to clean up, restart processes, and free the console that
458  * we've allocated. This routine is not called for hibernation.
459  */
suspend_finish(void)460 static void suspend_finish(void)
461 {
462 	suspend_thaw_processes();
463 	pm_notifier_call_chain(PM_POST_SUSPEND);
464 	pm_restore_console();
465 }
466 
467 /**
468  * enter_state - Do common work needed to enter system sleep state.
469  * @state: System sleep state to enter.
470  *
471  * Make sure that no one else is trying to put the system into a sleep state.
472  * Fail if that's not the case.  Otherwise, prepare for system suspend, make the
473  * system enter the given sleep state and clean up after wakeup.
474  */
enter_state(suspend_state_t state)475 static int enter_state(suspend_state_t state)
476 {
477 	int error;
478 
479 	trace_suspend_resume(TPS("suspend_enter"), state, true);
480 	if (state == PM_SUSPEND_FREEZE) {
481 #ifdef CONFIG_PM_DEBUG
482 		if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
483 			pr_warning("PM: Unsupported test mode for freeze state,"
484 				   "please choose none/freezer/devices/platform.\n");
485 			return -EAGAIN;
486 		}
487 #endif
488 	} else if (!valid_state(state)) {
489 		return -EINVAL;
490 	}
491 	if (!mutex_trylock(&pm_mutex))
492 		return -EBUSY;
493 
494 	if (state == PM_SUSPEND_FREEZE)
495 		freeze_begin();
496 
497 	trace_suspend_resume(TPS("sync_filesystems"), 0, true);
498 	printk(KERN_INFO "PM: Syncing filesystems ... ");
499 	sys_sync();
500 	printk("done.\n");
501 	trace_suspend_resume(TPS("sync_filesystems"), 0, false);
502 
503 	pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
504 	error = suspend_prepare(state);
505 	if (error)
506 		goto Unlock;
507 
508 	if (suspend_test(TEST_FREEZER))
509 		goto Finish;
510 
511 	trace_suspend_resume(TPS("suspend_enter"), state, false);
512 	pr_debug("PM: Entering %s sleep\n", pm_states[state]);
513 	pm_restrict_gfp_mask();
514 	error = suspend_devices_and_enter(state);
515 	pm_restore_gfp_mask();
516 
517  Finish:
518 	pr_debug("PM: Finishing wakeup.\n");
519 	suspend_finish();
520  Unlock:
521 	mutex_unlock(&pm_mutex);
522 	return error;
523 }
524 
pm_suspend_marker(char * annotation)525 static void pm_suspend_marker(char *annotation)
526 {
527 	struct timespec ts;
528 	struct tm tm;
529 
530 	getnstimeofday(&ts);
531 	time_to_tm(ts.tv_sec, 0, &tm);
532 	pr_info("PM: suspend %s %ld-%02d-%02d %02d:%02d:%02d.%09lu UTC\n",
533 		annotation, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
534 		tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
535 }
536 
537 /**
538  * pm_suspend - Externally visible function for suspending the system.
539  * @state: System sleep state to enter.
540  *
541  * Check if the value of @state represents one of the supported states,
542  * execute enter_state() and update system suspend statistics.
543  */
pm_suspend(suspend_state_t state)544 int pm_suspend(suspend_state_t state)
545 {
546 	int error;
547 
548 	if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
549 		return -EINVAL;
550 
551 	pm_suspend_marker("entry");
552 	error = enter_state(state);
553 	if (error) {
554 		suspend_stats.fail++;
555 		dpm_save_failed_errno(error);
556 	} else {
557 		suspend_stats.success++;
558 	}
559 	pm_suspend_marker("exit");
560 	return error;
561 }
562 EXPORT_SYMBOL(pm_suspend);
563