hibernate.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * kernel/power/hibernate.c - Hibernation (a.k.a 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@ucw.cz>
  7. * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
  8. *
  9. * This file is released under the GPLv2.
  10. */
  11. #include <linux/suspend.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/reboot.h>
  14. #include <linux/string.h>
  15. #include <linux/device.h>
  16. #include <linux/kmod.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. #include <linux/gfp.h>
  25. #include <scsi/scsi_scan.h>
  26. #include <asm/suspend.h>
  27. #include "power.h"
  28. static int noresume = 0;
  29. static char resume_file[256] = CONFIG_PM_STD_PARTITION;
  30. dev_t swsusp_resume_device;
  31. sector_t swsusp_resume_block;
  32. int in_suspend __nosavedata = 0;
  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. static int hibernation_mode = HIBERNATION_SHUTDOWN;
  46. static struct platform_hibernation_ops *hibernation_ops;
  47. /**
  48. * hibernation_set_ops - set the global hibernate operations
  49. * @ops: the hibernation operations to use in subsequent hibernation transitions
  50. */
  51. void hibernation_set_ops(struct platform_hibernation_ops *ops)
  52. {
  53. if (ops && !(ops->begin && ops->end && ops->pre_snapshot
  54. && ops->prepare && ops->finish && ops->enter && ops->pre_restore
  55. && ops->restore_cleanup)) {
  56. WARN_ON(1);
  57. return;
  58. }
  59. mutex_lock(&pm_mutex);
  60. hibernation_ops = ops;
  61. if (ops)
  62. hibernation_mode = HIBERNATION_PLATFORM;
  63. else if (hibernation_mode == HIBERNATION_PLATFORM)
  64. hibernation_mode = HIBERNATION_SHUTDOWN;
  65. mutex_unlock(&pm_mutex);
  66. }
  67. static bool entering_platform_hibernation;
  68. bool system_entering_hibernation(void)
  69. {
  70. return entering_platform_hibernation;
  71. }
  72. EXPORT_SYMBOL(system_entering_hibernation);
  73. #ifdef CONFIG_PM_DEBUG
  74. static void hibernation_debug_sleep(void)
  75. {
  76. printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
  77. mdelay(5000);
  78. }
  79. static int hibernation_testmode(int mode)
  80. {
  81. if (hibernation_mode == mode) {
  82. hibernation_debug_sleep();
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. static int hibernation_test(int level)
  88. {
  89. if (pm_test_level == level) {
  90. hibernation_debug_sleep();
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. #else /* !CONFIG_PM_DEBUG */
  96. static int hibernation_testmode(int mode) { return 0; }
  97. static int hibernation_test(int level) { return 0; }
  98. #endif /* !CONFIG_PM_DEBUG */
  99. /**
  100. * platform_begin - tell the platform driver that we're starting
  101. * hibernation
  102. */
  103. static int platform_begin(int platform_mode)
  104. {
  105. return (platform_mode && hibernation_ops) ?
  106. hibernation_ops->begin() : 0;
  107. }
  108. /**
  109. * platform_end - tell the platform driver that we've entered the
  110. * working state
  111. */
  112. static void platform_end(int platform_mode)
  113. {
  114. if (platform_mode && hibernation_ops)
  115. hibernation_ops->end();
  116. }
  117. /**
  118. * platform_pre_snapshot - prepare the machine for hibernation using the
  119. * platform driver if so configured and return an error code if it fails
  120. */
  121. static int platform_pre_snapshot(int platform_mode)
  122. {
  123. return (platform_mode && hibernation_ops) ?
  124. hibernation_ops->pre_snapshot() : 0;
  125. }
  126. /**
  127. * platform_leave - prepare the machine for switching to the normal mode
  128. * of operation using the platform driver (called with interrupts disabled)
  129. */
  130. static void platform_leave(int platform_mode)
  131. {
  132. if (platform_mode && hibernation_ops)
  133. hibernation_ops->leave();
  134. }
  135. /**
  136. * platform_finish - switch the machine to the normal mode of operation
  137. * using the platform driver (must be called after platform_prepare())
  138. */
  139. static void platform_finish(int platform_mode)
  140. {
  141. if (platform_mode && hibernation_ops)
  142. hibernation_ops->finish();
  143. }
  144. /**
  145. * platform_pre_restore - prepare the platform for the restoration from a
  146. * hibernation image. If the restore fails after this function has been
  147. * called, platform_restore_cleanup() must be called.
  148. */
  149. static int platform_pre_restore(int platform_mode)
  150. {
  151. return (platform_mode && hibernation_ops) ?
  152. hibernation_ops->pre_restore() : 0;
  153. }
  154. /**
  155. * platform_restore_cleanup - switch the platform to the normal mode of
  156. * operation after a failing restore. If platform_pre_restore() has been
  157. * called before the failing restore, this function must be called too,
  158. * regardless of the result of platform_pre_restore().
  159. */
  160. static void platform_restore_cleanup(int platform_mode)
  161. {
  162. if (platform_mode && hibernation_ops)
  163. hibernation_ops->restore_cleanup();
  164. }
  165. /**
  166. * platform_recover - recover the platform from a failure to suspend
  167. * devices.
  168. */
  169. static void platform_recover(int platform_mode)
  170. {
  171. if (platform_mode && hibernation_ops && hibernation_ops->recover)
  172. hibernation_ops->recover();
  173. }
  174. /**
  175. * swsusp_show_speed - print the time elapsed between two events.
  176. * @start: Starting event.
  177. * @stop: Final event.
  178. * @nr_pages - number of pages processed between @start and @stop
  179. * @msg - introductory message to print
  180. */
  181. void swsusp_show_speed(struct timeval *start, struct timeval *stop,
  182. unsigned nr_pages, char *msg)
  183. {
  184. s64 elapsed_centisecs64;
  185. int centisecs;
  186. int k;
  187. int kps;
  188. elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
  189. do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
  190. centisecs = elapsed_centisecs64;
  191. if (centisecs == 0)
  192. centisecs = 1; /* avoid div-by-zero */
  193. k = nr_pages * (PAGE_SIZE / 1024);
  194. kps = (k * 100) / centisecs;
  195. printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
  196. msg, k,
  197. centisecs / 100, centisecs % 100,
  198. kps / 1000, (kps % 1000) / 10);
  199. }
  200. /**
  201. * create_image - freeze devices that need to be frozen with interrupts
  202. * off, create the hibernation image and thaw those devices. Control
  203. * reappears in this routine after a restore.
  204. */
  205. static int create_image(int platform_mode)
  206. {
  207. int error;
  208. error = arch_prepare_suspend();
  209. if (error)
  210. return error;
  211. /* At this point, dpm_suspend_start() has been called, but *not*
  212. * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
  213. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  214. * become desynchronized with the actual state of the hardware
  215. * at resume time, and evil weirdness ensues.
  216. */
  217. error = dpm_suspend_noirq(PMSG_FREEZE);
  218. if (error) {
  219. printk(KERN_ERR "PM: Some devices failed to power down, "
  220. "aborting hibernation\n");
  221. return error;
  222. }
  223. error = platform_pre_snapshot(platform_mode);
  224. if (error || hibernation_test(TEST_PLATFORM))
  225. goto Platform_finish;
  226. error = disable_nonboot_cpus();
  227. if (error || hibernation_test(TEST_CPUS)
  228. || hibernation_testmode(HIBERNATION_TEST))
  229. goto Enable_cpus;
  230. local_irq_disable();
  231. error = sysdev_suspend(PMSG_FREEZE);
  232. if (error) {
  233. printk(KERN_ERR "PM: Some system devices failed to power down, "
  234. "aborting hibernation\n");
  235. goto Enable_irqs;
  236. }
  237. if (hibernation_test(TEST_CORE) || !pm_check_wakeup_events())
  238. goto Power_up;
  239. in_suspend = 1;
  240. save_processor_state();
  241. error = swsusp_arch_suspend();
  242. if (error)
  243. printk(KERN_ERR "PM: Error %d creating hibernation image\n",
  244. error);
  245. /* Restore control flow magically appears here */
  246. restore_processor_state();
  247. if (!in_suspend) {
  248. events_check_enabled = false;
  249. platform_leave(platform_mode);
  250. }
  251. Power_up:
  252. sysdev_resume();
  253. /* NOTE: dpm_resume_noirq() is just a resume() for devices
  254. * that suspended with irqs off ... no overall powerup.
  255. */
  256. Enable_irqs:
  257. local_irq_enable();
  258. Enable_cpus:
  259. enable_nonboot_cpus();
  260. Platform_finish:
  261. platform_finish(platform_mode);
  262. dpm_resume_noirq(in_suspend ?
  263. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  264. return error;
  265. }
  266. /**
  267. * hibernation_snapshot - quiesce devices and create the hibernation
  268. * snapshot image.
  269. * @platform_mode - if set, use the platform driver, if available, to
  270. * prepare the platform firmware for the power transition.
  271. *
  272. * Must be called with pm_mutex held
  273. */
  274. int hibernation_snapshot(int platform_mode)
  275. {
  276. int error;
  277. gfp_t saved_mask;
  278. error = platform_begin(platform_mode);
  279. if (error)
  280. goto Close;
  281. /* Preallocate image memory before shutting down devices. */
  282. error = hibernate_preallocate_memory();
  283. if (error)
  284. goto Close;
  285. suspend_console();
  286. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  287. error = dpm_suspend_start(PMSG_FREEZE);
  288. if (error)
  289. goto Recover_platform;
  290. if (hibernation_test(TEST_DEVICES))
  291. goto Recover_platform;
  292. error = create_image(platform_mode);
  293. /* Control returns here after successful restore */
  294. Resume_devices:
  295. /* We may need to release the preallocated image pages here. */
  296. if (error || !in_suspend)
  297. swsusp_free();
  298. dpm_resume_end(in_suspend ?
  299. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  300. set_gfp_allowed_mask(saved_mask);
  301. resume_console();
  302. Close:
  303. platform_end(platform_mode);
  304. return error;
  305. Recover_platform:
  306. platform_recover(platform_mode);
  307. goto Resume_devices;
  308. }
  309. /**
  310. * resume_target_kernel - prepare devices that need to be suspended with
  311. * interrupts off, restore the contents of highmem that have not been
  312. * restored yet from the image and run the low level code that will restore
  313. * the remaining contents of memory and switch to the just restored target
  314. * kernel.
  315. */
  316. static int resume_target_kernel(bool platform_mode)
  317. {
  318. int error;
  319. error = dpm_suspend_noirq(PMSG_QUIESCE);
  320. if (error) {
  321. printk(KERN_ERR "PM: Some devices failed to power down, "
  322. "aborting resume\n");
  323. return error;
  324. }
  325. error = platform_pre_restore(platform_mode);
  326. if (error)
  327. goto Cleanup;
  328. error = disable_nonboot_cpus();
  329. if (error)
  330. goto Enable_cpus;
  331. local_irq_disable();
  332. error = sysdev_suspend(PMSG_QUIESCE);
  333. if (error)
  334. goto Enable_irqs;
  335. /* We'll ignore saved state, but this gets preempt count (etc) right */
  336. save_processor_state();
  337. error = restore_highmem();
  338. if (!error) {
  339. error = swsusp_arch_resume();
  340. /*
  341. * The code below is only ever reached in case of a failure.
  342. * Otherwise execution continues at place where
  343. * swsusp_arch_suspend() was called
  344. */
  345. BUG_ON(!error);
  346. /* This call to restore_highmem() undos the previous one */
  347. restore_highmem();
  348. }
  349. /*
  350. * The only reason why swsusp_arch_resume() can fail is memory being
  351. * very tight, so we have to free it as soon as we can to avoid
  352. * subsequent failures
  353. */
  354. swsusp_free();
  355. restore_processor_state();
  356. touch_softlockup_watchdog();
  357. sysdev_resume();
  358. Enable_irqs:
  359. local_irq_enable();
  360. Enable_cpus:
  361. enable_nonboot_cpus();
  362. Cleanup:
  363. platform_restore_cleanup(platform_mode);
  364. dpm_resume_noirq(PMSG_RECOVER);
  365. return error;
  366. }
  367. /**
  368. * hibernation_restore - quiesce devices and restore the hibernation
  369. * snapshot image. If successful, control returns in hibernation_snaphot()
  370. * @platform_mode - if set, use the platform driver, if available, to
  371. * prepare the platform firmware for the transition.
  372. *
  373. * Must be called with pm_mutex held
  374. */
  375. int hibernation_restore(int platform_mode)
  376. {
  377. int error;
  378. gfp_t saved_mask;
  379. pm_prepare_console();
  380. suspend_console();
  381. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  382. error = dpm_suspend_start(PMSG_QUIESCE);
  383. if (!error) {
  384. error = resume_target_kernel(platform_mode);
  385. dpm_resume_end(PMSG_RECOVER);
  386. }
  387. set_gfp_allowed_mask(saved_mask);
  388. resume_console();
  389. pm_restore_console();
  390. return error;
  391. }
  392. /**
  393. * hibernation_platform_enter - enter the hibernation state using the
  394. * platform driver (if available)
  395. */
  396. int hibernation_platform_enter(void)
  397. {
  398. int error;
  399. gfp_t saved_mask;
  400. if (!hibernation_ops)
  401. return -ENOSYS;
  402. /*
  403. * We have cancelled the power transition by running
  404. * hibernation_ops->finish() before saving the image, so we should let
  405. * the firmware know that we're going to enter the sleep state after all
  406. */
  407. error = hibernation_ops->begin();
  408. if (error)
  409. goto Close;
  410. entering_platform_hibernation = true;
  411. suspend_console();
  412. saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
  413. error = dpm_suspend_start(PMSG_HIBERNATE);
  414. if (error) {
  415. if (hibernation_ops->recover)
  416. hibernation_ops->recover();
  417. goto Resume_devices;
  418. }
  419. error = dpm_suspend_noirq(PMSG_HIBERNATE);
  420. if (error)
  421. goto Resume_devices;
  422. error = hibernation_ops->prepare();
  423. if (error)
  424. goto Platform_finish;
  425. error = disable_nonboot_cpus();
  426. if (error)
  427. goto Platform_finish;
  428. local_irq_disable();
  429. sysdev_suspend(PMSG_HIBERNATE);
  430. if (!pm_check_wakeup_events()) {
  431. error = -EAGAIN;
  432. goto Power_up;
  433. }
  434. hibernation_ops->enter();
  435. /* We should never get here */
  436. while (1);
  437. Power_up:
  438. sysdev_resume();
  439. local_irq_enable();
  440. enable_nonboot_cpus();
  441. Platform_finish:
  442. hibernation_ops->finish();
  443. dpm_resume_noirq(PMSG_RESTORE);
  444. Resume_devices:
  445. entering_platform_hibernation = false;
  446. dpm_resume_end(PMSG_RESTORE);
  447. set_gfp_allowed_mask(saved_mask);
  448. resume_console();
  449. Close:
  450. hibernation_ops->end();
  451. return error;
  452. }
  453. /**
  454. * power_down - Shut the machine down for hibernation.
  455. *
  456. * Use the platform driver, if configured so; otherwise try
  457. * to power off or reboot.
  458. */
  459. static void power_down(void)
  460. {
  461. switch (hibernation_mode) {
  462. case HIBERNATION_TEST:
  463. case HIBERNATION_TESTPROC:
  464. break;
  465. case HIBERNATION_REBOOT:
  466. kernel_restart(NULL);
  467. break;
  468. case HIBERNATION_PLATFORM:
  469. hibernation_platform_enter();
  470. case HIBERNATION_SHUTDOWN:
  471. kernel_power_off();
  472. break;
  473. }
  474. kernel_halt();
  475. /*
  476. * Valid image is on the disk, if we continue we risk serious data
  477. * corruption after resume.
  478. */
  479. printk(KERN_CRIT "PM: Please power down manually\n");
  480. while(1);
  481. }
  482. static int prepare_processes(void)
  483. {
  484. int error = 0;
  485. if (freeze_processes()) {
  486. error = -EBUSY;
  487. thaw_processes();
  488. }
  489. return error;
  490. }
  491. /**
  492. * hibernate - The granpappy of the built-in hibernation management
  493. */
  494. int hibernate(void)
  495. {
  496. int error;
  497. mutex_lock(&pm_mutex);
  498. /* The snapshot device should not be opened while we're running */
  499. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  500. error = -EBUSY;
  501. goto Unlock;
  502. }
  503. pm_prepare_console();
  504. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  505. if (error)
  506. goto Exit;
  507. error = usermodehelper_disable();
  508. if (error)
  509. goto Exit;
  510. /* Allocate memory management structures */
  511. error = create_basic_memory_bitmaps();
  512. if (error)
  513. goto Exit;
  514. printk(KERN_INFO "PM: Syncing filesystems ... ");
  515. sys_sync();
  516. printk("done.\n");
  517. error = prepare_processes();
  518. if (error)
  519. goto Finish;
  520. if (hibernation_test(TEST_FREEZER))
  521. goto Thaw;
  522. if (hibernation_testmode(HIBERNATION_TESTPROC))
  523. goto Thaw;
  524. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  525. if (error)
  526. goto Thaw;
  527. if (in_suspend) {
  528. unsigned int flags = 0;
  529. if (hibernation_mode == HIBERNATION_PLATFORM)
  530. flags |= SF_PLATFORM_MODE;
  531. pr_debug("PM: writing image.\n");
  532. error = swsusp_write(flags);
  533. swsusp_free();
  534. if (!error)
  535. power_down();
  536. } else {
  537. pr_debug("PM: Image restored successfully.\n");
  538. }
  539. Thaw:
  540. thaw_processes();
  541. Finish:
  542. free_basic_memory_bitmaps();
  543. usermodehelper_enable();
  544. Exit:
  545. pm_notifier_call_chain(PM_POST_HIBERNATION);
  546. pm_restore_console();
  547. atomic_inc(&snapshot_device_available);
  548. Unlock:
  549. mutex_unlock(&pm_mutex);
  550. return error;
  551. }
  552. /**
  553. * software_resume - Resume from a saved image.
  554. *
  555. * Called as a late_initcall (so all devices are discovered and
  556. * initialized), we call swsusp to see if we have a saved image or not.
  557. * If so, we quiesce devices, the restore the saved image. We will
  558. * return above (in hibernate() ) if everything goes well.
  559. * Otherwise, we fail gracefully and return to the normally
  560. * scheduled program.
  561. *
  562. */
  563. static int software_resume(void)
  564. {
  565. int error;
  566. unsigned int flags;
  567. /*
  568. * If the user said "noresume".. bail out early.
  569. */
  570. if (noresume)
  571. return 0;
  572. /*
  573. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  574. * is configured into the kernel. Since the regular hibernate
  575. * trigger path is via sysfs which takes a buffer mutex before
  576. * calling hibernate functions (which take pm_mutex) this can
  577. * cause lockdep to complain about a possible ABBA deadlock
  578. * which cannot happen since we're in the boot code here and
  579. * sysfs can't be invoked yet. Therefore, we use a subclass
  580. * here to avoid lockdep complaining.
  581. */
  582. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  583. if (swsusp_resume_device)
  584. goto Check_image;
  585. if (!strlen(resume_file)) {
  586. error = -ENOENT;
  587. goto Unlock;
  588. }
  589. pr_debug("PM: Checking image partition %s\n", resume_file);
  590. /* Check if the device is there */
  591. swsusp_resume_device = name_to_dev_t(resume_file);
  592. if (!swsusp_resume_device) {
  593. /*
  594. * Some device discovery might still be in progress; we need
  595. * to wait for this to finish.
  596. */
  597. wait_for_device_probe();
  598. /*
  599. * We can't depend on SCSI devices being available after loading
  600. * one of their modules until scsi_complete_async_scans() is
  601. * called and the resume device usually is a SCSI one.
  602. */
  603. scsi_complete_async_scans();
  604. swsusp_resume_device = name_to_dev_t(resume_file);
  605. if (!swsusp_resume_device) {
  606. error = -ENODEV;
  607. goto Unlock;
  608. }
  609. }
  610. Check_image:
  611. pr_debug("PM: Resume from partition %d:%d\n",
  612. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  613. pr_debug("PM: Checking hibernation image.\n");
  614. error = swsusp_check();
  615. if (error)
  616. goto Unlock;
  617. /* The snapshot device should not be opened while we're running */
  618. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  619. error = -EBUSY;
  620. swsusp_close(FMODE_READ);
  621. goto Unlock;
  622. }
  623. pm_prepare_console();
  624. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  625. if (error)
  626. goto close_finish;
  627. error = usermodehelper_disable();
  628. if (error)
  629. goto close_finish;
  630. error = create_basic_memory_bitmaps();
  631. if (error)
  632. goto close_finish;
  633. pr_debug("PM: Preparing processes for restore.\n");
  634. error = prepare_processes();
  635. if (error) {
  636. swsusp_close(FMODE_READ);
  637. goto Done;
  638. }
  639. pr_debug("PM: Reading hibernation image.\n");
  640. error = swsusp_read(&flags);
  641. swsusp_close(FMODE_READ);
  642. if (!error)
  643. hibernation_restore(flags & SF_PLATFORM_MODE);
  644. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  645. swsusp_free();
  646. thaw_processes();
  647. Done:
  648. free_basic_memory_bitmaps();
  649. usermodehelper_enable();
  650. Finish:
  651. pm_notifier_call_chain(PM_POST_RESTORE);
  652. pm_restore_console();
  653. atomic_inc(&snapshot_device_available);
  654. /* For success case, the suspend path will release the lock */
  655. Unlock:
  656. mutex_unlock(&pm_mutex);
  657. pr_debug("PM: Resume from disk failed.\n");
  658. return error;
  659. close_finish:
  660. swsusp_close(FMODE_READ);
  661. goto Finish;
  662. }
  663. late_initcall(software_resume);
  664. static const char * const hibernation_modes[] = {
  665. [HIBERNATION_PLATFORM] = "platform",
  666. [HIBERNATION_SHUTDOWN] = "shutdown",
  667. [HIBERNATION_REBOOT] = "reboot",
  668. [HIBERNATION_TEST] = "test",
  669. [HIBERNATION_TESTPROC] = "testproc",
  670. };
  671. /**
  672. * disk - Control hibernation mode
  673. *
  674. * Suspend-to-disk can be handled in several ways. We have a few options
  675. * for putting the system to sleep - using the platform driver (e.g. ACPI
  676. * or other hibernation_ops), powering off the system or rebooting the
  677. * system (for testing) as well as the two test modes.
  678. *
  679. * The system can support 'platform', and that is known a priori (and
  680. * encoded by the presence of hibernation_ops). However, the user may
  681. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  682. * test modes, 'test' or 'testproc'.
  683. *
  684. * show() will display what the mode is currently set to.
  685. * store() will accept one of
  686. *
  687. * 'platform'
  688. * 'shutdown'
  689. * 'reboot'
  690. * 'test'
  691. * 'testproc'
  692. *
  693. * It will only change to 'platform' if the system
  694. * supports it (as determined by having hibernation_ops).
  695. */
  696. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  697. char *buf)
  698. {
  699. int i;
  700. char *start = buf;
  701. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  702. if (!hibernation_modes[i])
  703. continue;
  704. switch (i) {
  705. case HIBERNATION_SHUTDOWN:
  706. case HIBERNATION_REBOOT:
  707. case HIBERNATION_TEST:
  708. case HIBERNATION_TESTPROC:
  709. break;
  710. case HIBERNATION_PLATFORM:
  711. if (hibernation_ops)
  712. break;
  713. /* not a valid mode, continue with loop */
  714. continue;
  715. }
  716. if (i == hibernation_mode)
  717. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  718. else
  719. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  720. }
  721. buf += sprintf(buf, "\n");
  722. return buf-start;
  723. }
  724. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  725. const char *buf, size_t n)
  726. {
  727. int error = 0;
  728. int i;
  729. int len;
  730. char *p;
  731. int mode = HIBERNATION_INVALID;
  732. p = memchr(buf, '\n', n);
  733. len = p ? p - buf : n;
  734. mutex_lock(&pm_mutex);
  735. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  736. if (len == strlen(hibernation_modes[i])
  737. && !strncmp(buf, hibernation_modes[i], len)) {
  738. mode = i;
  739. break;
  740. }
  741. }
  742. if (mode != HIBERNATION_INVALID) {
  743. switch (mode) {
  744. case HIBERNATION_SHUTDOWN:
  745. case HIBERNATION_REBOOT:
  746. case HIBERNATION_TEST:
  747. case HIBERNATION_TESTPROC:
  748. hibernation_mode = mode;
  749. break;
  750. case HIBERNATION_PLATFORM:
  751. if (hibernation_ops)
  752. hibernation_mode = mode;
  753. else
  754. error = -EINVAL;
  755. }
  756. } else
  757. error = -EINVAL;
  758. if (!error)
  759. pr_debug("PM: Hibernation mode set to '%s'\n",
  760. hibernation_modes[mode]);
  761. mutex_unlock(&pm_mutex);
  762. return error ? error : n;
  763. }
  764. power_attr(disk);
  765. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  766. char *buf)
  767. {
  768. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  769. MINOR(swsusp_resume_device));
  770. }
  771. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  772. const char *buf, size_t n)
  773. {
  774. unsigned int maj, min;
  775. dev_t res;
  776. int ret = -EINVAL;
  777. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  778. goto out;
  779. res = MKDEV(maj,min);
  780. if (maj != MAJOR(res) || min != MINOR(res))
  781. goto out;
  782. mutex_lock(&pm_mutex);
  783. swsusp_resume_device = res;
  784. mutex_unlock(&pm_mutex);
  785. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  786. noresume = 0;
  787. software_resume();
  788. ret = n;
  789. out:
  790. return ret;
  791. }
  792. power_attr(resume);
  793. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  794. char *buf)
  795. {
  796. return sprintf(buf, "%lu\n", image_size);
  797. }
  798. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  799. const char *buf, size_t n)
  800. {
  801. unsigned long size;
  802. if (sscanf(buf, "%lu", &size) == 1) {
  803. image_size = size;
  804. return n;
  805. }
  806. return -EINVAL;
  807. }
  808. power_attr(image_size);
  809. static struct attribute * g[] = {
  810. &disk_attr.attr,
  811. &resume_attr.attr,
  812. &image_size_attr.attr,
  813. NULL,
  814. };
  815. static struct attribute_group attr_group = {
  816. .attrs = g,
  817. };
  818. static int __init pm_disk_init(void)
  819. {
  820. return sysfs_create_group(power_kobj, &attr_group);
  821. }
  822. core_initcall(pm_disk_init);
  823. static int __init resume_setup(char *str)
  824. {
  825. if (noresume)
  826. return 1;
  827. strncpy( resume_file, str, 255 );
  828. return 1;
  829. }
  830. static int __init resume_offset_setup(char *str)
  831. {
  832. unsigned long long offset;
  833. if (noresume)
  834. return 1;
  835. if (sscanf(str, "%llu", &offset) == 1)
  836. swsusp_resume_block = offset;
  837. return 1;
  838. }
  839. static int __init noresume_setup(char *str)
  840. {
  841. noresume = 1;
  842. return 1;
  843. }
  844. __setup("noresume", noresume_setup);
  845. __setup("resume_offset=", resume_offset_setup);
  846. __setup("resume=", resume_setup);