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