hibernate.c 25 KB

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