Fix ThinkPad T42 poweroff failure introduced by by "PM: Introduce pm_power_off_prepare"
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / power / disk.c
1 /*
2 * kernel/power/disk.c - Suspend-to-disk support.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12 #include <linux/suspend.h>
13 #include <linux/syscalls.h>
14 #include <linux/reboot.h>
15 #include <linux/string.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/fs.h>
19 #include <linux/mount.h>
20 #include <linux/pm.h>
21 #include <linux/console.h>
22 #include <linux/cpu.h>
23 #include <linux/freezer.h>
24
25 #include "power.h"
26
27
28 static int noresume = 0;
29 char resume_file[256] = CONFIG_PM_STD_PARTITION;
30 dev_t swsusp_resume_device;
31 sector_t swsusp_resume_block;
32
33 enum {
34 HIBERNATION_INVALID,
35 HIBERNATION_PLATFORM,
36 HIBERNATION_TEST,
37 HIBERNATION_TESTPROC,
38 HIBERNATION_SHUTDOWN,
39 HIBERNATION_REBOOT,
40 /* keep last */
41 __HIBERNATION_AFTER_LAST
42 };
43 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
44 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
45
46 static int hibernation_mode = HIBERNATION_SHUTDOWN;
47
48 static struct hibernation_ops *hibernation_ops;
49
50 /**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
55 void hibernation_set_ops(struct hibernation_ops *ops)
56 {
57 if (ops && !(ops->prepare && ops->enter && ops->finish
58 && ops->pre_restore && ops->restore_cleanup)) {
59 WARN_ON(1);
60 return;
61 }
62 mutex_lock(&pm_mutex);
63 hibernation_ops = ops;
64 if (ops)
65 hibernation_mode = HIBERNATION_PLATFORM;
66 else if (hibernation_mode == HIBERNATION_PLATFORM)
67 hibernation_mode = HIBERNATION_SHUTDOWN;
68
69 mutex_unlock(&pm_mutex);
70 }
71
72
73 /**
74 * platform_prepare - prepare the machine for hibernation using the
75 * platform driver if so configured and return an error code if it fails
76 */
77
78 static int platform_prepare(int platform_mode)
79 {
80 return (platform_mode && hibernation_ops) ?
81 hibernation_ops->prepare() : 0;
82 }
83
84 /**
85 * platform_finish - switch the machine to the normal mode of operation
86 * using the platform driver (must be called after platform_prepare())
87 */
88
89 static void platform_finish(int platform_mode)
90 {
91 if (platform_mode && hibernation_ops)
92 hibernation_ops->finish();
93 }
94
95 /**
96 * platform_pre_restore - prepare the platform for the restoration from a
97 * hibernation image. If the restore fails after this function has been
98 * called, platform_restore_cleanup() must be called.
99 */
100
101 static int platform_pre_restore(int platform_mode)
102 {
103 return (platform_mode && hibernation_ops) ?
104 hibernation_ops->pre_restore() : 0;
105 }
106
107 /**
108 * platform_restore_cleanup - switch the platform to the normal mode of
109 * operation after a failing restore. If platform_pre_restore() has been
110 * called before the failing restore, this function must be called too,
111 * regardless of the result of platform_pre_restore().
112 */
113
114 static void platform_restore_cleanup(int platform_mode)
115 {
116 if (platform_mode && hibernation_ops)
117 hibernation_ops->restore_cleanup();
118 }
119
120 /**
121 * hibernation_snapshot - quiesce devices and create the hibernation
122 * snapshot image.
123 * @platform_mode - if set, use the platform driver, if available, to
124 * prepare the platform frimware for the power transition.
125 *
126 * Must be called with pm_mutex held
127 */
128
129 int hibernation_snapshot(int platform_mode)
130 {
131 int error;
132
133 /* Free memory before shutting down devices. */
134 error = swsusp_shrink_memory();
135 if (error)
136 return error;
137
138 suspend_console();
139 error = device_suspend(PMSG_FREEZE);
140 if (error)
141 goto Resume_console;
142
143 error = platform_prepare(platform_mode);
144 if (error)
145 goto Resume_devices;
146
147 error = disable_nonboot_cpus();
148 if (!error) {
149 if (hibernation_mode != HIBERNATION_TEST) {
150 in_suspend = 1;
151 error = swsusp_suspend();
152 /* Control returns here after successful restore */
153 } else {
154 printk("swsusp debug: Waiting for 5 seconds.\n");
155 mdelay(5000);
156 }
157 }
158 enable_nonboot_cpus();
159 Resume_devices:
160 platform_finish(platform_mode);
161 device_resume();
162 Resume_console:
163 resume_console();
164 return error;
165 }
166
167 /**
168 * hibernation_restore - quiesce devices and restore the hibernation
169 * snapshot image. If successful, control returns in hibernation_snaphot()
170 * @platform_mode - if set, use the platform driver, if available, to
171 * prepare the platform frimware for the transition.
172 *
173 * Must be called with pm_mutex held
174 */
175
176 int hibernation_restore(int platform_mode)
177 {
178 int error;
179
180 pm_prepare_console();
181 suspend_console();
182 error = device_suspend(PMSG_PRETHAW);
183 if (error)
184 goto Finish;
185
186 error = platform_pre_restore(platform_mode);
187 if (!error) {
188 error = disable_nonboot_cpus();
189 if (!error)
190 error = swsusp_resume();
191 enable_nonboot_cpus();
192 }
193 platform_restore_cleanup(platform_mode);
194 device_resume();
195 Finish:
196 resume_console();
197 pm_restore_console();
198 return error;
199 }
200
201 /**
202 * hibernation_platform_enter - enter the hibernation state using the
203 * platform driver (if available)
204 */
205
206 int hibernation_platform_enter(void)
207 {
208 int error;
209
210 if (hibernation_ops) {
211 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
212 /*
213 * We have cancelled the power transition by running
214 * hibernation_ops->finish() before saving the image, so we
215 * should let the firmware know that we're going to enter the
216 * sleep state after all
217 */
218 error = hibernation_ops->prepare();
219 sysdev_shutdown();
220 if (!error)
221 error = hibernation_ops->enter();
222 } else {
223 error = -ENOSYS;
224 }
225 return error;
226 }
227
228 /**
229 * power_down - Shut the machine down for hibernation.
230 *
231 * Use the platform driver, if configured so; otherwise try
232 * to power off or reboot.
233 */
234
235 static void power_down(void)
236 {
237 switch (hibernation_mode) {
238 case HIBERNATION_TEST:
239 case HIBERNATION_TESTPROC:
240 break;
241 case HIBERNATION_SHUTDOWN:
242 kernel_power_off();
243 break;
244 case HIBERNATION_REBOOT:
245 kernel_restart(NULL);
246 break;
247 case HIBERNATION_PLATFORM:
248 hibernation_platform_enter();
249 }
250 kernel_halt();
251 /*
252 * Valid image is on the disk, if we continue we risk serious data
253 * corruption after resume.
254 */
255 printk(KERN_CRIT "Please power me down manually\n");
256 while(1);
257 }
258
259 static void unprepare_processes(void)
260 {
261 thaw_processes();
262 pm_restore_console();
263 }
264
265 static int prepare_processes(void)
266 {
267 int error = 0;
268
269 pm_prepare_console();
270 if (freeze_processes()) {
271 error = -EBUSY;
272 unprepare_processes();
273 }
274 return error;
275 }
276
277 /**
278 * hibernate - The granpappy of the built-in hibernation management
279 */
280
281 int hibernate(void)
282 {
283 int error;
284
285 mutex_lock(&pm_mutex);
286 /* The snapshot device should not be opened while we're running */
287 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
288 error = -EBUSY;
289 goto Unlock;
290 }
291
292 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
293 if (error)
294 goto Exit;
295
296 /* Allocate memory management structures */
297 error = create_basic_memory_bitmaps();
298 if (error)
299 goto Exit;
300
301 error = prepare_processes();
302 if (error)
303 goto Finish;
304
305 if (hibernation_mode == HIBERNATION_TESTPROC) {
306 printk("swsusp debug: Waiting for 5 seconds.\n");
307 mdelay(5000);
308 goto Thaw;
309 }
310 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
311 if (in_suspend && !error) {
312 unsigned int flags = 0;
313
314 if (hibernation_mode == HIBERNATION_PLATFORM)
315 flags |= SF_PLATFORM_MODE;
316 pr_debug("PM: writing image.\n");
317 error = swsusp_write(flags);
318 swsusp_free();
319 if (!error)
320 power_down();
321 } else {
322 pr_debug("PM: Image restored successfully.\n");
323 swsusp_free();
324 }
325 Thaw:
326 unprepare_processes();
327 Finish:
328 free_basic_memory_bitmaps();
329 Exit:
330 pm_notifier_call_chain(PM_POST_HIBERNATION);
331 atomic_inc(&snapshot_device_available);
332 Unlock:
333 mutex_unlock(&pm_mutex);
334 return error;
335 }
336
337
338 /**
339 * software_resume - Resume from a saved image.
340 *
341 * Called as a late_initcall (so all devices are discovered and
342 * initialized), we call swsusp to see if we have a saved image or not.
343 * If so, we quiesce devices, the restore the saved image. We will
344 * return above (in hibernate() ) if everything goes well.
345 * Otherwise, we fail gracefully and return to the normally
346 * scheduled program.
347 *
348 */
349
350 static int software_resume(void)
351 {
352 int error;
353 unsigned int flags;
354
355 mutex_lock(&pm_mutex);
356 if (!swsusp_resume_device) {
357 if (!strlen(resume_file)) {
358 mutex_unlock(&pm_mutex);
359 return -ENOENT;
360 }
361 swsusp_resume_device = name_to_dev_t(resume_file);
362 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
363 } else {
364 pr_debug("swsusp: Resume From Partition %d:%d\n",
365 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
366 }
367
368 if (noresume) {
369 /**
370 * FIXME: If noresume is specified, we need to find the partition
371 * and reset it back to normal swap space.
372 */
373 mutex_unlock(&pm_mutex);
374 return 0;
375 }
376
377 pr_debug("PM: Checking swsusp image.\n");
378 error = swsusp_check();
379 if (error)
380 goto Unlock;
381
382 /* The snapshot device should not be opened while we're running */
383 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
384 error = -EBUSY;
385 goto Unlock;
386 }
387
388 error = create_basic_memory_bitmaps();
389 if (error)
390 goto Finish;
391
392 pr_debug("PM: Preparing processes for restore.\n");
393 error = prepare_processes();
394 if (error) {
395 swsusp_close();
396 goto Done;
397 }
398
399 pr_debug("PM: Reading swsusp image.\n");
400
401 error = swsusp_read(&flags);
402 if (!error)
403 hibernation_restore(flags & SF_PLATFORM_MODE);
404
405 printk(KERN_ERR "PM: Restore failed, recovering.\n");
406 swsusp_free();
407 unprepare_processes();
408 Done:
409 free_basic_memory_bitmaps();
410 Finish:
411 atomic_inc(&snapshot_device_available);
412 /* For success case, the suspend path will release the lock */
413 Unlock:
414 mutex_unlock(&pm_mutex);
415 pr_debug("PM: Resume from disk failed.\n");
416 return error;
417 }
418
419 late_initcall(software_resume);
420
421
422 static const char * const hibernation_modes[] = {
423 [HIBERNATION_PLATFORM] = "platform",
424 [HIBERNATION_SHUTDOWN] = "shutdown",
425 [HIBERNATION_REBOOT] = "reboot",
426 [HIBERNATION_TEST] = "test",
427 [HIBERNATION_TESTPROC] = "testproc",
428 };
429
430 /**
431 * disk - Control hibernation mode
432 *
433 * Suspend-to-disk can be handled in several ways. We have a few options
434 * for putting the system to sleep - using the platform driver (e.g. ACPI
435 * or other hibernation_ops), powering off the system or rebooting the
436 * system (for testing) as well as the two test modes.
437 *
438 * The system can support 'platform', and that is known a priori (and
439 * encoded by the presence of hibernation_ops). However, the user may
440 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
441 * test modes, 'test' or 'testproc'.
442 *
443 * show() will display what the mode is currently set to.
444 * store() will accept one of
445 *
446 * 'platform'
447 * 'shutdown'
448 * 'reboot'
449 * 'test'
450 * 'testproc'
451 *
452 * It will only change to 'platform' if the system
453 * supports it (as determined by having hibernation_ops).
454 */
455
456 static ssize_t disk_show(struct kset *kset, char *buf)
457 {
458 int i;
459 char *start = buf;
460
461 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
462 if (!hibernation_modes[i])
463 continue;
464 switch (i) {
465 case HIBERNATION_SHUTDOWN:
466 case HIBERNATION_REBOOT:
467 case HIBERNATION_TEST:
468 case HIBERNATION_TESTPROC:
469 break;
470 case HIBERNATION_PLATFORM:
471 if (hibernation_ops)
472 break;
473 /* not a valid mode, continue with loop */
474 continue;
475 }
476 if (i == hibernation_mode)
477 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
478 else
479 buf += sprintf(buf, "%s ", hibernation_modes[i]);
480 }
481 buf += sprintf(buf, "\n");
482 return buf-start;
483 }
484
485
486 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
487 {
488 int error = 0;
489 int i;
490 int len;
491 char *p;
492 int mode = HIBERNATION_INVALID;
493
494 p = memchr(buf, '\n', n);
495 len = p ? p - buf : n;
496
497 mutex_lock(&pm_mutex);
498 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
499 if (len == strlen(hibernation_modes[i])
500 && !strncmp(buf, hibernation_modes[i], len)) {
501 mode = i;
502 break;
503 }
504 }
505 if (mode != HIBERNATION_INVALID) {
506 switch (mode) {
507 case HIBERNATION_SHUTDOWN:
508 case HIBERNATION_REBOOT:
509 case HIBERNATION_TEST:
510 case HIBERNATION_TESTPROC:
511 hibernation_mode = mode;
512 break;
513 case HIBERNATION_PLATFORM:
514 if (hibernation_ops)
515 hibernation_mode = mode;
516 else
517 error = -EINVAL;
518 }
519 } else
520 error = -EINVAL;
521
522 if (!error)
523 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
524 hibernation_modes[mode]);
525 mutex_unlock(&pm_mutex);
526 return error ? error : n;
527 }
528
529 power_attr(disk);
530
531 static ssize_t resume_show(struct kset *kset, char *buf)
532 {
533 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
534 MINOR(swsusp_resume_device));
535 }
536
537 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
538 {
539 unsigned int maj, min;
540 dev_t res;
541 int ret = -EINVAL;
542
543 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
544 goto out;
545
546 res = MKDEV(maj,min);
547 if (maj != MAJOR(res) || min != MINOR(res))
548 goto out;
549
550 mutex_lock(&pm_mutex);
551 swsusp_resume_device = res;
552 mutex_unlock(&pm_mutex);
553 printk("Attempting manual resume\n");
554 noresume = 0;
555 software_resume();
556 ret = n;
557 out:
558 return ret;
559 }
560
561 power_attr(resume);
562
563 static ssize_t image_size_show(struct kset *kset, char *buf)
564 {
565 return sprintf(buf, "%lu\n", image_size);
566 }
567
568 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
569 {
570 unsigned long size;
571
572 if (sscanf(buf, "%lu", &size) == 1) {
573 image_size = size;
574 return n;
575 }
576
577 return -EINVAL;
578 }
579
580 power_attr(image_size);
581
582 static struct attribute * g[] = {
583 &disk_attr.attr,
584 &resume_attr.attr,
585 &image_size_attr.attr,
586 NULL,
587 };
588
589
590 static struct attribute_group attr_group = {
591 .attrs = g,
592 };
593
594
595 static int __init pm_disk_init(void)
596 {
597 return sysfs_create_group(&power_subsys.kobj, &attr_group);
598 }
599
600 core_initcall(pm_disk_init);
601
602
603 static int __init resume_setup(char *str)
604 {
605 if (noresume)
606 return 1;
607
608 strncpy( resume_file, str, 255 );
609 return 1;
610 }
611
612 static int __init resume_offset_setup(char *str)
613 {
614 unsigned long long offset;
615
616 if (noresume)
617 return 1;
618
619 if (sscanf(str, "%llu", &offset) == 1)
620 swsusp_resume_block = offset;
621
622 return 1;
623 }
624
625 static int __init noresume_setup(char *str)
626 {
627 noresume = 1;
628 return 1;
629 }
630
631 __setup("noresume", noresume_setup);
632 __setup("resume_offset=", resume_offset_setup);
633 __setup("resume=", resume_setup);