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/export.h>
12 #include <linux/kobject.h>
13 #include <linux/string.h>
14 #include <linux/resume-trace.h>
15 #include <linux/workqueue.h>
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
18 
19 #include "power.h"
20 
21 DEFINE_MUTEX(pm_mutex);
22 
23 #ifdef CONFIG_PM_SLEEP
24 
25 /* Routines for PM-transition notifications */
26 
27 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28 
register_pm_notifier(struct notifier_block * nb)29 int register_pm_notifier(struct notifier_block *nb)
30 {
31 	return blocking_notifier_chain_register(&pm_chain_head, nb);
32 }
33 EXPORT_SYMBOL_GPL(register_pm_notifier);
34 
unregister_pm_notifier(struct notifier_block * nb)35 int unregister_pm_notifier(struct notifier_block *nb)
36 {
37 	return blocking_notifier_chain_unregister(&pm_chain_head, nb);
38 }
39 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
40 
pm_notifier_call_chain(unsigned long val)41 int pm_notifier_call_chain(unsigned long val)
42 {
43 	int ret = blocking_notifier_call_chain(&pm_chain_head, val, NULL);
44 
45 	return notifier_to_errno(ret);
46 }
47 
48 /* If set, devices may be suspended and resumed asynchronously. */
49 int pm_async_enabled = 1;
50 
pm_async_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)51 static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
52 			     char *buf)
53 {
54 	return sprintf(buf, "%d\n", pm_async_enabled);
55 }
56 
pm_async_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)57 static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
58 			      const char *buf, size_t n)
59 {
60 	unsigned long val;
61 
62 	if (strict_strtoul(buf, 10, &val))
63 		return -EINVAL;
64 
65 	if (val > 1)
66 		return -EINVAL;
67 
68 	pm_async_enabled = val;
69 	return n;
70 }
71 
72 power_attr(pm_async);
73 
74 #ifdef CONFIG_PM_DEBUG
75 int pm_test_level = TEST_NONE;
76 
77 static const char * const pm_tests[__TEST_AFTER_LAST] = {
78 	[TEST_NONE] = "none",
79 	[TEST_CORE] = "core",
80 	[TEST_CPUS] = "processors",
81 	[TEST_PLATFORM] = "platform",
82 	[TEST_DEVICES] = "devices",
83 	[TEST_FREEZER] = "freezer",
84 };
85 
pm_test_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)86 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
87 				char *buf)
88 {
89 	char *s = buf;
90 	int level;
91 
92 	for (level = TEST_FIRST; level <= TEST_MAX; level++)
93 		if (pm_tests[level]) {
94 			if (level == pm_test_level)
95 				s += sprintf(s, "[%s] ", pm_tests[level]);
96 			else
97 				s += sprintf(s, "%s ", pm_tests[level]);
98 		}
99 
100 	if (s != buf)
101 		/* convert the last space to a newline */
102 		*(s-1) = '\n';
103 
104 	return (s - buf);
105 }
106 
pm_test_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)107 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
108 				const char *buf, size_t n)
109 {
110 	const char * const *s;
111 	int level;
112 	char *p;
113 	int len;
114 	int error = -EINVAL;
115 
116 	p = memchr(buf, '\n', n);
117 	len = p ? p - buf : n;
118 
119 	lock_system_sleep();
120 
121 	level = TEST_FIRST;
122 	for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
123 		if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
124 			pm_test_level = level;
125 			error = 0;
126 			break;
127 		}
128 
129 	unlock_system_sleep();
130 
131 	return error ? error : n;
132 }
133 
134 power_attr(pm_test);
135 #endif /* CONFIG_PM_DEBUG */
136 
137 #ifdef CONFIG_DEBUG_FS
suspend_step_name(enum suspend_stat_step step)138 static char *suspend_step_name(enum suspend_stat_step step)
139 {
140 	switch (step) {
141 	case SUSPEND_FREEZE:
142 		return "freeze";
143 	case SUSPEND_PREPARE:
144 		return "prepare";
145 	case SUSPEND_SUSPEND:
146 		return "suspend";
147 	case SUSPEND_SUSPEND_NOIRQ:
148 		return "suspend_noirq";
149 	case SUSPEND_RESUME_NOIRQ:
150 		return "resume_noirq";
151 	case SUSPEND_RESUME:
152 		return "resume";
153 	default:
154 		return "";
155 	}
156 }
157 
suspend_stats_show(struct seq_file * s,void * unused)158 static int suspend_stats_show(struct seq_file *s, void *unused)
159 {
160 	int i, index, last_dev, last_errno, last_step;
161 
162 	last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
163 	last_dev %= REC_FAILED_NUM;
164 	last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
165 	last_errno %= REC_FAILED_NUM;
166 	last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
167 	last_step %= REC_FAILED_NUM;
168 	seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
169 			"%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
170 			"success", suspend_stats.success,
171 			"fail", suspend_stats.fail,
172 			"failed_freeze", suspend_stats.failed_freeze,
173 			"failed_prepare", suspend_stats.failed_prepare,
174 			"failed_suspend", suspend_stats.failed_suspend,
175 			"failed_suspend_late",
176 				suspend_stats.failed_suspend_late,
177 			"failed_suspend_noirq",
178 				suspend_stats.failed_suspend_noirq,
179 			"failed_resume", suspend_stats.failed_resume,
180 			"failed_resume_early",
181 				suspend_stats.failed_resume_early,
182 			"failed_resume_noirq",
183 				suspend_stats.failed_resume_noirq);
184 	seq_printf(s,	"failures:\n  last_failed_dev:\t%-s\n",
185 			suspend_stats.failed_devs[last_dev]);
186 	for (i = 1; i < REC_FAILED_NUM; i++) {
187 		index = last_dev + REC_FAILED_NUM - i;
188 		index %= REC_FAILED_NUM;
189 		seq_printf(s, "\t\t\t%-s\n",
190 			suspend_stats.failed_devs[index]);
191 	}
192 	seq_printf(s,	"  last_failed_errno:\t%-d\n",
193 			suspend_stats.errno[last_errno]);
194 	for (i = 1; i < REC_FAILED_NUM; i++) {
195 		index = last_errno + REC_FAILED_NUM - i;
196 		index %= REC_FAILED_NUM;
197 		seq_printf(s, "\t\t\t%-d\n",
198 			suspend_stats.errno[index]);
199 	}
200 	seq_printf(s,	"  last_failed_step:\t%-s\n",
201 			suspend_step_name(
202 				suspend_stats.failed_steps[last_step]));
203 	for (i = 1; i < REC_FAILED_NUM; i++) {
204 		index = last_step + REC_FAILED_NUM - i;
205 		index %= REC_FAILED_NUM;
206 		seq_printf(s, "\t\t\t%-s\n",
207 			suspend_step_name(
208 				suspend_stats.failed_steps[index]));
209 	}
210 
211 	return 0;
212 }
213 
suspend_stats_open(struct inode * inode,struct file * file)214 static int suspend_stats_open(struct inode *inode, struct file *file)
215 {
216 	return single_open(file, suspend_stats_show, NULL);
217 }
218 
219 static const struct file_operations suspend_stats_operations = {
220 	.open           = suspend_stats_open,
221 	.read           = seq_read,
222 	.llseek         = seq_lseek,
223 	.release        = single_release,
224 };
225 
pm_debugfs_init(void)226 static int __init pm_debugfs_init(void)
227 {
228 	debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
229 			NULL, NULL, &suspend_stats_operations);
230 	return 0;
231 }
232 
233 late_initcall(pm_debugfs_init);
234 #endif /* CONFIG_DEBUG_FS */
235 
236 #endif /* CONFIG_PM_SLEEP */
237 
238 struct kobject *power_kobj;
239 
240 /**
241  *	state - control system power state.
242  *
243  *	show() returns what states are supported, which is hard-coded to
244  *	'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
245  *	'disk' (Suspend-to-Disk).
246  *
247  *	store() accepts one of those strings, translates it into the
248  *	proper enumerated value, and initiates a suspend transition.
249  */
state_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)250 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
251 			  char *buf)
252 {
253 	char *s = buf;
254 #ifdef CONFIG_SUSPEND
255 	int i;
256 
257 	for (i = 0; i < PM_SUSPEND_MAX; i++) {
258 		if (pm_states[i] && valid_state(i))
259 			s += sprintf(s,"%s ", pm_states[i]);
260 	}
261 #endif
262 #ifdef CONFIG_HIBERNATION
263 	s += sprintf(s, "%s\n", "disk");
264 #else
265 	if (s != buf)
266 		/* convert the last space to a newline */
267 		*(s-1) = '\n';
268 #endif
269 	return (s - buf);
270 }
271 
state_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)272 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
273 			   const char *buf, size_t n)
274 {
275 #ifdef CONFIG_SUSPEND
276 	suspend_state_t state = PM_SUSPEND_STANDBY;
277 	const char * const *s;
278 #endif
279 	char *p;
280 	int len;
281 	int error = -EINVAL;
282 
283 	p = memchr(buf, '\n', n);
284 	len = p ? p - buf : n;
285 
286 	/* First, check if we are requested to hibernate */
287 	if (len == 4 && !strncmp(buf, "disk", len)) {
288 		error = hibernate();
289 		goto Exit;
290 	}
291 
292 #ifdef CONFIG_SUSPEND
293 	for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
294 		if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
295 			error = pm_suspend(state);
296 			break;
297 		}
298 	}
299 #endif
300 
301  Exit:
302 	return error ? error : n;
303 }
304 
305 power_attr(state);
306 
307 #ifdef CONFIG_PM_SLEEP
308 /*
309  * The 'wakeup_count' attribute, along with the functions defined in
310  * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
311  * handled in a non-racy way.
312  *
313  * If a wakeup event occurs when the system is in a sleep state, it simply is
314  * woken up.  In turn, if an event that would wake the system up from a sleep
315  * state occurs when it is undergoing a transition to that sleep state, the
316  * transition should be aborted.  Moreover, if such an event occurs when the
317  * system is in the working state, an attempt to start a transition to the
318  * given sleep state should fail during certain period after the detection of
319  * the event.  Using the 'state' attribute alone is not sufficient to satisfy
320  * these requirements, because a wakeup event may occur exactly when 'state'
321  * is being written to and may be delivered to user space right before it is
322  * frozen, so the event will remain only partially processed until the system is
323  * woken up by another event.  In particular, it won't cause the transition to
324  * a sleep state to be aborted.
325  *
326  * This difficulty may be overcome if user space uses 'wakeup_count' before
327  * writing to 'state'.  It first should read from 'wakeup_count' and store
328  * the read value.  Then, after carrying out its own preparations for the system
329  * transition to a sleep state, it should write the stored value to
330  * 'wakeup_count'.  If that fails, at least one wakeup event has occurred since
331  * 'wakeup_count' was read and 'state' should not be written to.  Otherwise, it
332  * is allowed to write to 'state', but the transition will be aborted if there
333  * are any wakeup events detected after 'wakeup_count' was written to.
334  */
335 
wakeup_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)336 static ssize_t wakeup_count_show(struct kobject *kobj,
337 				struct kobj_attribute *attr,
338 				char *buf)
339 {
340 	unsigned int val;
341 
342 	return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR;
343 }
344 
wakeup_count_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)345 static ssize_t wakeup_count_store(struct kobject *kobj,
346 				struct kobj_attribute *attr,
347 				const char *buf, size_t n)
348 {
349 	unsigned int val;
350 
351 	if (sscanf(buf, "%u", &val) == 1) {
352 		if (pm_save_wakeup_count(val))
353 			return n;
354 	}
355 	return -EINVAL;
356 }
357 
358 power_attr(wakeup_count);
359 #endif /* CONFIG_PM_SLEEP */
360 
361 #ifdef CONFIG_PM_TRACE
362 int pm_trace_enabled;
363 
pm_trace_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)364 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
365 			     char *buf)
366 {
367 	return sprintf(buf, "%d\n", pm_trace_enabled);
368 }
369 
370 static ssize_t
pm_trace_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)371 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
372 	       const char *buf, size_t n)
373 {
374 	int val;
375 
376 	if (sscanf(buf, "%d", &val) == 1) {
377 		pm_trace_enabled = !!val;
378 		return n;
379 	}
380 	return -EINVAL;
381 }
382 
383 power_attr(pm_trace);
384 
pm_trace_dev_match_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)385 static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
386 				       struct kobj_attribute *attr,
387 				       char *buf)
388 {
389 	return show_trace_dev_match(buf, PAGE_SIZE);
390 }
391 
392 static ssize_t
pm_trace_dev_match_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)393 pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr,
394 			 const char *buf, size_t n)
395 {
396 	return -EINVAL;
397 }
398 
399 power_attr(pm_trace_dev_match);
400 
401 #endif /* CONFIG_PM_TRACE */
402 
403 static struct attribute * g[] = {
404 	&state_attr.attr,
405 #ifdef CONFIG_PM_TRACE
406 	&pm_trace_attr.attr,
407 	&pm_trace_dev_match_attr.attr,
408 #endif
409 #ifdef CONFIG_PM_SLEEP
410 	&pm_async_attr.attr,
411 	&wakeup_count_attr.attr,
412 #ifdef CONFIG_PM_DEBUG
413 	&pm_test_attr.attr,
414 #endif
415 #endif
416 	NULL,
417 };
418 
419 static struct attribute_group attr_group = {
420 	.attrs = g,
421 };
422 
423 #ifdef CONFIG_PM_RUNTIME
424 struct workqueue_struct *pm_wq;
425 EXPORT_SYMBOL_GPL(pm_wq);
426 
pm_start_workqueue(void)427 static int __init pm_start_workqueue(void)
428 {
429 	pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
430 
431 	return pm_wq ? 0 : -ENOMEM;
432 }
433 #else
pm_start_workqueue(void)434 static inline int pm_start_workqueue(void) { return 0; }
435 #endif
436 
pm_init(void)437 static int __init pm_init(void)
438 {
439 	int error = pm_start_workqueue();
440 	if (error)
441 		return error;
442 	hibernate_image_size_init();
443 	hibernate_reserved_size_init();
444 	power_kobj = kobject_create_and_add("power", NULL);
445 	if (!power_kobj)
446 		return -ENOMEM;
447 	return sysfs_create_group(power_kobj, &attr_group);
448 }
449 
450 core_initcall(pm_init);
451