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