xref: /linux/kernel/power/main.c (revision 0c93ea4064a209cdc36de8a9a3003d43d08f46f7)
1 /*
2  * kernel/power/main.c - PM subsystem core functionality.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2
8  *
9  */
10 
11 #include <linux/module.h>
12 #include <linux/suspend.h>
13 #include <linux/kobject.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/kmod.h>
18 #include <linux/init.h>
19 #include <linux/console.h>
20 #include <linux/cpu.h>
21 #include <linux/resume-trace.h>
22 #include <linux/freezer.h>
23 #include <linux/vmstat.h>
24 #include <linux/syscalls.h>
25 
26 #include "power.h"
27 
28 DEFINE_MUTEX(pm_mutex);
29 
30 unsigned int pm_flags;
31 EXPORT_SYMBOL(pm_flags);
32 
33 #ifdef CONFIG_PM_SLEEP
34 
35 /* Routines for PM-transition notifications */
36 
37 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
38 
39 int register_pm_notifier(struct notifier_block *nb)
40 {
41 	return blocking_notifier_chain_register(&pm_chain_head, nb);
42 }
43 EXPORT_SYMBOL_GPL(register_pm_notifier);
44 
45 int unregister_pm_notifier(struct notifier_block *nb)
46 {
47 	return blocking_notifier_chain_unregister(&pm_chain_head, nb);
48 }
49 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
50 
51 int pm_notifier_call_chain(unsigned long val)
52 {
53 	return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
54 			== NOTIFY_BAD) ? -EINVAL : 0;
55 }
56 
57 #ifdef CONFIG_PM_DEBUG
58 int pm_test_level = TEST_NONE;
59 
60 static const char * const pm_tests[__TEST_AFTER_LAST] = {
61 	[TEST_NONE] = "none",
62 	[TEST_CORE] = "core",
63 	[TEST_CPUS] = "processors",
64 	[TEST_PLATFORM] = "platform",
65 	[TEST_DEVICES] = "devices",
66 	[TEST_FREEZER] = "freezer",
67 };
68 
69 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
70 				char *buf)
71 {
72 	char *s = buf;
73 	int level;
74 
75 	for (level = TEST_FIRST; level <= TEST_MAX; level++)
76 		if (pm_tests[level]) {
77 			if (level == pm_test_level)
78 				s += sprintf(s, "[%s] ", pm_tests[level]);
79 			else
80 				s += sprintf(s, "%s ", pm_tests[level]);
81 		}
82 
83 	if (s != buf)
84 		/* convert the last space to a newline */
85 		*(s-1) = '\n';
86 
87 	return (s - buf);
88 }
89 
90 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
91 				const char *buf, size_t n)
92 {
93 	const char * const *s;
94 	int level;
95 	char *p;
96 	int len;
97 	int error = -EINVAL;
98 
99 	p = memchr(buf, '\n', n);
100 	len = p ? p - buf : n;
101 
102 	mutex_lock(&pm_mutex);
103 
104 	level = TEST_FIRST;
105 	for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
106 		if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
107 			pm_test_level = level;
108 			error = 0;
109 			break;
110 		}
111 
112 	mutex_unlock(&pm_mutex);
113 
114 	return error ? error : n;
115 }
116 
117 power_attr(pm_test);
118 #endif /* CONFIG_PM_DEBUG */
119 
120 #endif /* CONFIG_PM_SLEEP */
121 
122 #ifdef CONFIG_SUSPEND
123 
124 static int suspend_test(int level)
125 {
126 #ifdef CONFIG_PM_DEBUG
127 	if (pm_test_level == level) {
128 		printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
129 		mdelay(5000);
130 		return 1;
131 	}
132 #endif /* !CONFIG_PM_DEBUG */
133 	return 0;
134 }
135 
136 #ifdef CONFIG_PM_TEST_SUSPEND
137 
138 /*
139  * We test the system suspend code by setting an RTC wakealarm a short
140  * time in the future, then suspending.  Suspending the devices won't
141  * normally take long ... some systems only need a few milliseconds.
142  *
143  * The time it takes is system-specific though, so when we test this
144  * during system bootup we allow a LOT of time.
145  */
146 #define TEST_SUSPEND_SECONDS	5
147 
148 static unsigned long suspend_test_start_time;
149 
150 static void suspend_test_start(void)
151 {
152 	/* FIXME Use better timebase than "jiffies", ideally a clocksource.
153 	 * What we want is a hardware counter that will work correctly even
154 	 * during the irqs-are-off stages of the suspend/resume cycle...
155 	 */
156 	suspend_test_start_time = jiffies;
157 }
158 
159 static void suspend_test_finish(const char *label)
160 {
161 	long nj = jiffies - suspend_test_start_time;
162 	unsigned msec;
163 
164 	msec = jiffies_to_msecs(abs(nj));
165 	pr_info("PM: %s took %d.%03d seconds\n", label,
166 			msec / 1000, msec % 1000);
167 
168 	/* Warning on suspend means the RTC alarm period needs to be
169 	 * larger -- the system was sooo slooowwww to suspend that the
170 	 * alarm (should have) fired before the system went to sleep!
171 	 *
172 	 * Warning on either suspend or resume also means the system
173 	 * has some performance issues.  The stack dump of a WARN_ON
174 	 * is more likely to get the right attention than a printk...
175 	 */
176 	WARN(msec > (TEST_SUSPEND_SECONDS * 1000), "Component: %s\n", label);
177 }
178 
179 #else
180 
181 static void suspend_test_start(void)
182 {
183 }
184 
185 static void suspend_test_finish(const char *label)
186 {
187 }
188 
189 #endif
190 
191 /* This is just an arbitrary number */
192 #define FREE_PAGE_NUMBER (100)
193 
194 static struct platform_suspend_ops *suspend_ops;
195 
196 /**
197  *	suspend_set_ops - Set the global suspend method table.
198  *	@ops:	Pointer to ops structure.
199  */
200 
201 void suspend_set_ops(struct platform_suspend_ops *ops)
202 {
203 	mutex_lock(&pm_mutex);
204 	suspend_ops = ops;
205 	mutex_unlock(&pm_mutex);
206 }
207 
208 /**
209  * suspend_valid_only_mem - generic memory-only valid callback
210  *
211  * Platform drivers that implement mem suspend only and only need
212  * to check for that in their .valid callback can use this instead
213  * of rolling their own .valid callback.
214  */
215 int suspend_valid_only_mem(suspend_state_t state)
216 {
217 	return state == PM_SUSPEND_MEM;
218 }
219 
220 /**
221  *	suspend_prepare - Do prep work before entering low-power state.
222  *
223  *	This is common code that is called for each state that we're entering.
224  *	Run suspend notifiers, allocate a console and stop all processes.
225  */
226 static int suspend_prepare(void)
227 {
228 	int error;
229 	unsigned int free_pages;
230 
231 	if (!suspend_ops || !suspend_ops->enter)
232 		return -EPERM;
233 
234 	pm_prepare_console();
235 
236 	error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
237 	if (error)
238 		goto Finish;
239 
240 	error = usermodehelper_disable();
241 	if (error)
242 		goto Finish;
243 
244 	if (suspend_freeze_processes()) {
245 		error = -EAGAIN;
246 		goto Thaw;
247 	}
248 
249 	free_pages = global_page_state(NR_FREE_PAGES);
250 	if (free_pages < FREE_PAGE_NUMBER) {
251 		pr_debug("PM: free some memory\n");
252 		shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
253 		if (nr_free_pages() < FREE_PAGE_NUMBER) {
254 			error = -ENOMEM;
255 			printk(KERN_ERR "PM: No enough memory\n");
256 		}
257 	}
258 	if (!error)
259 		return 0;
260 
261  Thaw:
262 	suspend_thaw_processes();
263 	usermodehelper_enable();
264  Finish:
265 	pm_notifier_call_chain(PM_POST_SUSPEND);
266 	pm_restore_console();
267 	return error;
268 }
269 
270 /* default implementation */
271 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
272 {
273 	local_irq_disable();
274 }
275 
276 /* default implementation */
277 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
278 {
279 	local_irq_enable();
280 }
281 
282 /**
283  *	suspend_enter - enter the desired system sleep state.
284  *	@state:		state to enter
285  *
286  *	This function should be called after devices have been suspended.
287  */
288 static int suspend_enter(suspend_state_t state)
289 {
290 	int error = 0;
291 
292 	device_pm_lock();
293 	arch_suspend_disable_irqs();
294 	BUG_ON(!irqs_disabled());
295 
296 	if ((error = device_power_down(PMSG_SUSPEND))) {
297 		printk(KERN_ERR "PM: Some devices failed to power down\n");
298 		goto Done;
299 	}
300 
301 	error = sysdev_suspend(PMSG_SUSPEND);
302 	if (!error) {
303 		if (!suspend_test(TEST_CORE))
304 			error = suspend_ops->enter(state);
305 		sysdev_resume();
306 	}
307 
308 	device_power_up(PMSG_RESUME);
309  Done:
310 	arch_suspend_enable_irqs();
311 	BUG_ON(irqs_disabled());
312 	device_pm_unlock();
313 	return error;
314 }
315 
316 /**
317  *	suspend_devices_and_enter - suspend devices and enter the desired system
318  *				    sleep state.
319  *	@state:		  state to enter
320  */
321 int suspend_devices_and_enter(suspend_state_t state)
322 {
323 	int error;
324 
325 	if (!suspend_ops)
326 		return -ENOSYS;
327 
328 	if (suspend_ops->begin) {
329 		error = suspend_ops->begin(state);
330 		if (error)
331 			goto Close;
332 	}
333 	suspend_console();
334 	suspend_test_start();
335 	error = device_suspend(PMSG_SUSPEND);
336 	if (error) {
337 		printk(KERN_ERR "PM: Some devices failed to suspend\n");
338 		goto Recover_platform;
339 	}
340 	suspend_test_finish("suspend devices");
341 	if (suspend_test(TEST_DEVICES))
342 		goto Recover_platform;
343 
344 	if (suspend_ops->prepare) {
345 		error = suspend_ops->prepare();
346 		if (error)
347 			goto Resume_devices;
348 	}
349 
350 	if (suspend_test(TEST_PLATFORM))
351 		goto Finish;
352 
353 	error = disable_nonboot_cpus();
354 	if (!error && !suspend_test(TEST_CPUS))
355 		suspend_enter(state);
356 
357 	enable_nonboot_cpus();
358  Finish:
359 	if (suspend_ops->finish)
360 		suspend_ops->finish();
361  Resume_devices:
362 	suspend_test_start();
363 	device_resume(PMSG_RESUME);
364 	suspend_test_finish("resume devices");
365 	resume_console();
366  Close:
367 	if (suspend_ops->end)
368 		suspend_ops->end();
369 	return error;
370 
371  Recover_platform:
372 	if (suspend_ops->recover)
373 		suspend_ops->recover();
374 	goto Resume_devices;
375 }
376 
377 /**
378  *	suspend_finish - Do final work before exiting suspend sequence.
379  *
380  *	Call platform code to clean up, restart processes, and free the
381  *	console that we've allocated. This is not called for suspend-to-disk.
382  */
383 static void suspend_finish(void)
384 {
385 	suspend_thaw_processes();
386 	usermodehelper_enable();
387 	pm_notifier_call_chain(PM_POST_SUSPEND);
388 	pm_restore_console();
389 }
390 
391 
392 
393 
394 static const char * const pm_states[PM_SUSPEND_MAX] = {
395 	[PM_SUSPEND_STANDBY]	= "standby",
396 	[PM_SUSPEND_MEM]	= "mem",
397 };
398 
399 static inline int valid_state(suspend_state_t state)
400 {
401 	/* All states need lowlevel support and need to be valid
402 	 * to the lowlevel implementation, no valid callback
403 	 * implies that none are valid. */
404 	if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
405 		return 0;
406 	return 1;
407 }
408 
409 
410 /**
411  *	enter_state - Do common work of entering low-power state.
412  *	@state:		pm_state structure for state we're entering.
413  *
414  *	Make sure we're the only ones trying to enter a sleep state. Fail
415  *	if someone has beat us to it, since we don't want anything weird to
416  *	happen when we wake up.
417  *	Then, do the setup for suspend, enter the state, and cleaup (after
418  *	we've woken up).
419  */
420 static int enter_state(suspend_state_t state)
421 {
422 	int error;
423 
424 	if (!valid_state(state))
425 		return -ENODEV;
426 
427 	if (!mutex_trylock(&pm_mutex))
428 		return -EBUSY;
429 
430 	printk(KERN_INFO "PM: Syncing filesystems ... ");
431 	sys_sync();
432 	printk("done.\n");
433 
434 	pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
435 	error = suspend_prepare();
436 	if (error)
437 		goto Unlock;
438 
439 	if (suspend_test(TEST_FREEZER))
440 		goto Finish;
441 
442 	pr_debug("PM: Entering %s sleep\n", pm_states[state]);
443 	error = suspend_devices_and_enter(state);
444 
445  Finish:
446 	pr_debug("PM: Finishing wakeup.\n");
447 	suspend_finish();
448  Unlock:
449 	mutex_unlock(&pm_mutex);
450 	return error;
451 }
452 
453 
454 /**
455  *	pm_suspend - Externally visible function for suspending system.
456  *	@state:		Enumerated value of state to enter.
457  *
458  *	Determine whether or not value is within range, get state
459  *	structure, and enter (above).
460  */
461 
462 int pm_suspend(suspend_state_t state)
463 {
464 	if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
465 		return enter_state(state);
466 	return -EINVAL;
467 }
468 
469 EXPORT_SYMBOL(pm_suspend);
470 
471 #endif /* CONFIG_SUSPEND */
472 
473 struct kobject *power_kobj;
474 
475 /**
476  *	state - control system power state.
477  *
478  *	show() returns what states are supported, which is hard-coded to
479  *	'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
480  *	'disk' (Suspend-to-Disk).
481  *
482  *	store() accepts one of those strings, translates it into the
483  *	proper enumerated value, and initiates a suspend transition.
484  */
485 
486 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
487 			  char *buf)
488 {
489 	char *s = buf;
490 #ifdef CONFIG_SUSPEND
491 	int i;
492 
493 	for (i = 0; i < PM_SUSPEND_MAX; i++) {
494 		if (pm_states[i] && valid_state(i))
495 			s += sprintf(s,"%s ", pm_states[i]);
496 	}
497 #endif
498 #ifdef CONFIG_HIBERNATION
499 	s += sprintf(s, "%s\n", "disk");
500 #else
501 	if (s != buf)
502 		/* convert the last space to a newline */
503 		*(s-1) = '\n';
504 #endif
505 	return (s - buf);
506 }
507 
508 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
509 			   const char *buf, size_t n)
510 {
511 #ifdef CONFIG_SUSPEND
512 	suspend_state_t state = PM_SUSPEND_STANDBY;
513 	const char * const *s;
514 #endif
515 	char *p;
516 	int len;
517 	int error = -EINVAL;
518 
519 	p = memchr(buf, '\n', n);
520 	len = p ? p - buf : n;
521 
522 	/* First, check if we are requested to hibernate */
523 	if (len == 4 && !strncmp(buf, "disk", len)) {
524 		error = hibernate();
525   goto Exit;
526 	}
527 
528 #ifdef CONFIG_SUSPEND
529 	for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
530 		if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
531 			break;
532 	}
533 	if (state < PM_SUSPEND_MAX && *s)
534 		error = enter_state(state);
535 #endif
536 
537  Exit:
538 	return error ? error : n;
539 }
540 
541 power_attr(state);
542 
543 #ifdef CONFIG_PM_TRACE
544 int pm_trace_enabled;
545 
546 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
547 			     char *buf)
548 {
549 	return sprintf(buf, "%d\n", pm_trace_enabled);
550 }
551 
552 static ssize_t
553 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
554 	       const char *buf, size_t n)
555 {
556 	int val;
557 
558 	if (sscanf(buf, "%d", &val) == 1) {
559 		pm_trace_enabled = !!val;
560 		return n;
561 	}
562 	return -EINVAL;
563 }
564 
565 power_attr(pm_trace);
566 #endif /* CONFIG_PM_TRACE */
567 
568 static struct attribute * g[] = {
569 	&state_attr.attr,
570 #ifdef CONFIG_PM_TRACE
571 	&pm_trace_attr.attr,
572 #endif
573 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
574 	&pm_test_attr.attr,
575 #endif
576 	NULL,
577 };
578 
579 static struct attribute_group attr_group = {
580 	.attrs = g,
581 };
582 
583 
584 static int __init pm_init(void)
585 {
586 	power_kobj = kobject_create_and_add("power", NULL);
587 	if (!power_kobj)
588 		return -ENOMEM;
589 	return sysfs_create_group(power_kobj, &attr_group);
590 }
591 
592 core_initcall(pm_init);
593 
594 
595 #ifdef CONFIG_PM_TEST_SUSPEND
596 
597 #include <linux/rtc.h>
598 
599 /*
600  * To test system suspend, we need a hands-off mechanism to resume the
601  * system.  RTCs wake alarms are a common self-contained mechanism.
602  */
603 
604 static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
605 {
606 	static char err_readtime[] __initdata =
607 		KERN_ERR "PM: can't read %s time, err %d\n";
608 	static char err_wakealarm [] __initdata =
609 		KERN_ERR "PM: can't set %s wakealarm, err %d\n";
610 	static char err_suspend[] __initdata =
611 		KERN_ERR "PM: suspend test failed, error %d\n";
612 	static char info_test[] __initdata =
613 		KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
614 
615 	unsigned long		now;
616 	struct rtc_wkalrm	alm;
617 	int			status;
618 
619 	/* this may fail if the RTC hasn't been initialized */
620 	status = rtc_read_time(rtc, &alm.time);
621 	if (status < 0) {
622 		printk(err_readtime, dev_name(&rtc->dev), status);
623 		return;
624 	}
625 	rtc_tm_to_time(&alm.time, &now);
626 
627 	memset(&alm, 0, sizeof alm);
628 	rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
629 	alm.enabled = true;
630 
631 	status = rtc_set_alarm(rtc, &alm);
632 	if (status < 0) {
633 		printk(err_wakealarm, dev_name(&rtc->dev), status);
634 		return;
635 	}
636 
637 	if (state == PM_SUSPEND_MEM) {
638 		printk(info_test, pm_states[state]);
639 		status = pm_suspend(state);
640 		if (status == -ENODEV)
641 			state = PM_SUSPEND_STANDBY;
642 	}
643 	if (state == PM_SUSPEND_STANDBY) {
644 		printk(info_test, pm_states[state]);
645 		status = pm_suspend(state);
646 	}
647 	if (status < 0)
648 		printk(err_suspend, status);
649 
650 	/* Some platforms can't detect that the alarm triggered the
651 	 * wakeup, or (accordingly) disable it after it afterwards.
652 	 * It's supposed to give oneshot behavior; cope.
653 	 */
654 	alm.enabled = false;
655 	rtc_set_alarm(rtc, &alm);
656 }
657 
658 static int __init has_wakealarm(struct device *dev, void *name_ptr)
659 {
660 	struct rtc_device *candidate = to_rtc_device(dev);
661 
662 	if (!candidate->ops->set_alarm)
663 		return 0;
664 	if (!device_may_wakeup(candidate->dev.parent))
665 		return 0;
666 
667 	*(const char **)name_ptr = dev_name(dev);
668 	return 1;
669 }
670 
671 /*
672  * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
673  * at startup time.  They're normally disabled, for faster boot and because
674  * we can't know which states really work on this particular system.
675  */
676 static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
677 
678 static char warn_bad_state[] __initdata =
679 	KERN_WARNING "PM: can't test '%s' suspend state\n";
680 
681 static int __init setup_test_suspend(char *value)
682 {
683 	unsigned i;
684 
685 	/* "=mem" ==> "mem" */
686 	value++;
687 	for (i = 0; i < PM_SUSPEND_MAX; i++) {
688 		if (!pm_states[i])
689 			continue;
690 		if (strcmp(pm_states[i], value) != 0)
691 			continue;
692 		test_state = (__force suspend_state_t) i;
693 		return 0;
694 	}
695 	printk(warn_bad_state, value);
696 	return 0;
697 }
698 __setup("test_suspend", setup_test_suspend);
699 
700 static int __init test_suspend(void)
701 {
702 	static char		warn_no_rtc[] __initdata =
703 		KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
704 
705 	char			*pony = NULL;
706 	struct rtc_device	*rtc = NULL;
707 
708 	/* PM is initialized by now; is that state testable? */
709 	if (test_state == PM_SUSPEND_ON)
710 		goto done;
711 	if (!valid_state(test_state)) {
712 		printk(warn_bad_state, pm_states[test_state]);
713 		goto done;
714 	}
715 
716 	/* RTCs have initialized by now too ... can we use one? */
717 	class_find_device(rtc_class, NULL, &pony, has_wakealarm);
718 	if (pony)
719 		rtc = rtc_class_open(pony);
720 	if (!rtc) {
721 		printk(warn_no_rtc);
722 		goto done;
723 	}
724 
725 	/* go for it */
726 	test_wakealarm(rtc, test_state);
727 	rtc_class_close(rtc);
728 done:
729 	return 0;
730 }
731 late_initcall(test_suspend);
732 
733 #endif /* CONFIG_PM_TEST_SUSPEND */
734