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