hibernate.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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 Cleanup;
  302. }
  303. error = dpm_prepare(PMSG_FREEZE);
  304. if (error) {
  305. dpm_complete(PMSG_RECOVER);
  306. goto Cleanup;
  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. Cleanup:
  333. swsusp_free();
  334. goto Close;
  335. }
  336. /**
  337. * resume_target_kernel - Restore system state from a hibernation image.
  338. * @platform_mode: Whether or not to use the platform driver.
  339. *
  340. * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
  341. * contents of highmem that have not been restored yet from the image and run
  342. * the low-level code that will restore the remaining contents of memory and
  343. * switch to the just restored target kernel.
  344. */
  345. static int resume_target_kernel(bool platform_mode)
  346. {
  347. int error;
  348. error = dpm_suspend_end(PMSG_QUIESCE);
  349. if (error) {
  350. printk(KERN_ERR "PM: Some devices failed to power down, "
  351. "aborting resume\n");
  352. return error;
  353. }
  354. error = platform_pre_restore(platform_mode);
  355. if (error)
  356. goto Cleanup;
  357. error = disable_nonboot_cpus();
  358. if (error)
  359. goto Enable_cpus;
  360. local_irq_disable();
  361. error = syscore_suspend();
  362. if (error)
  363. goto Enable_irqs;
  364. save_processor_state();
  365. error = restore_highmem();
  366. if (!error) {
  367. error = swsusp_arch_resume();
  368. /*
  369. * The code below is only ever reached in case of a failure.
  370. * Otherwise, execution continues at the place where
  371. * swsusp_arch_suspend() was called.
  372. */
  373. BUG_ON(!error);
  374. /*
  375. * This call to restore_highmem() reverts the changes made by
  376. * the previous one.
  377. */
  378. restore_highmem();
  379. }
  380. /*
  381. * The only reason why swsusp_arch_resume() can fail is memory being
  382. * very tight, so we have to free it as soon as we can to avoid
  383. * subsequent failures.
  384. */
  385. swsusp_free();
  386. restore_processor_state();
  387. touch_softlockup_watchdog();
  388. syscore_resume();
  389. Enable_irqs:
  390. local_irq_enable();
  391. Enable_cpus:
  392. enable_nonboot_cpus();
  393. Cleanup:
  394. platform_restore_cleanup(platform_mode);
  395. dpm_resume_start(PMSG_RECOVER);
  396. return error;
  397. }
  398. /**
  399. * hibernation_restore - Quiesce devices and restore from a hibernation image.
  400. * @platform_mode: If set, use platform driver to prepare for the transition.
  401. *
  402. * This routine must be called with pm_mutex held. If it is successful, control
  403. * reappears in the restored target kernel in hibernation_snapshot().
  404. */
  405. int hibernation_restore(int platform_mode)
  406. {
  407. int error;
  408. pm_prepare_console();
  409. suspend_console();
  410. pm_restrict_gfp_mask();
  411. error = dpm_suspend_start(PMSG_QUIESCE);
  412. if (!error) {
  413. error = resume_target_kernel(platform_mode);
  414. dpm_resume_end(PMSG_RECOVER);
  415. }
  416. pm_restore_gfp_mask();
  417. resume_console();
  418. pm_restore_console();
  419. return error;
  420. }
  421. /**
  422. * hibernation_platform_enter - Power off the system using the platform driver.
  423. */
  424. int hibernation_platform_enter(void)
  425. {
  426. int error;
  427. if (!hibernation_ops)
  428. return -ENOSYS;
  429. /*
  430. * We have cancelled the power transition by running
  431. * hibernation_ops->finish() before saving the image, so we should let
  432. * the firmware know that we're going to enter the sleep state after all
  433. */
  434. error = hibernation_ops->begin();
  435. if (error)
  436. goto Close;
  437. entering_platform_hibernation = true;
  438. suspend_console();
  439. error = dpm_suspend_start(PMSG_HIBERNATE);
  440. if (error) {
  441. if (hibernation_ops->recover)
  442. hibernation_ops->recover();
  443. goto Resume_devices;
  444. }
  445. error = dpm_suspend_end(PMSG_HIBERNATE);
  446. if (error)
  447. goto Resume_devices;
  448. error = hibernation_ops->prepare();
  449. if (error)
  450. goto Platform_finish;
  451. error = disable_nonboot_cpus();
  452. if (error)
  453. goto Platform_finish;
  454. local_irq_disable();
  455. syscore_suspend();
  456. if (pm_wakeup_pending()) {
  457. error = -EAGAIN;
  458. goto Power_up;
  459. }
  460. hibernation_ops->enter();
  461. /* We should never get here */
  462. while (1);
  463. Power_up:
  464. syscore_resume();
  465. local_irq_enable();
  466. enable_nonboot_cpus();
  467. Platform_finish:
  468. hibernation_ops->finish();
  469. dpm_resume_start(PMSG_RESTORE);
  470. Resume_devices:
  471. entering_platform_hibernation = false;
  472. dpm_resume_end(PMSG_RESTORE);
  473. resume_console();
  474. Close:
  475. hibernation_ops->end();
  476. return error;
  477. }
  478. /**
  479. * power_down - Shut the machine down for hibernation.
  480. *
  481. * Use the platform driver, if configured, to put the system into the sleep
  482. * state corresponding to hibernation, or try to power it off or reboot,
  483. * depending on the value of hibernation_mode.
  484. */
  485. static void power_down(void)
  486. {
  487. switch (hibernation_mode) {
  488. case HIBERNATION_REBOOT:
  489. kernel_restart(NULL);
  490. break;
  491. case HIBERNATION_PLATFORM:
  492. hibernation_platform_enter();
  493. case HIBERNATION_SHUTDOWN:
  494. kernel_power_off();
  495. break;
  496. }
  497. kernel_halt();
  498. /*
  499. * Valid image is on the disk, if we continue we risk serious data
  500. * corruption after resume.
  501. */
  502. printk(KERN_CRIT "PM: Please power down manually\n");
  503. while(1);
  504. }
  505. /**
  506. * hibernate - Carry out system hibernation, including saving the image.
  507. */
  508. int hibernate(void)
  509. {
  510. int error;
  511. lock_system_sleep();
  512. /* The snapshot device should not be opened while we're running */
  513. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  514. error = -EBUSY;
  515. goto Unlock;
  516. }
  517. pm_prepare_console();
  518. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  519. if (error)
  520. goto Exit;
  521. error = usermodehelper_disable();
  522. if (error)
  523. goto Exit;
  524. /* Allocate memory management structures */
  525. error = create_basic_memory_bitmaps();
  526. if (error)
  527. goto Exit;
  528. printk(KERN_INFO "PM: Syncing filesystems ... ");
  529. sys_sync();
  530. printk("done.\n");
  531. error = freeze_processes();
  532. if (error)
  533. goto Finish;
  534. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  535. if (error)
  536. goto Thaw;
  537. if (freezer_test_done) {
  538. freezer_test_done = false;
  539. goto Thaw;
  540. }
  541. if (in_suspend) {
  542. unsigned int flags = 0;
  543. if (hibernation_mode == HIBERNATION_PLATFORM)
  544. flags |= SF_PLATFORM_MODE;
  545. if (nocompress)
  546. flags |= SF_NOCOMPRESS_MODE;
  547. else
  548. flags |= SF_CRC32_MODE;
  549. pr_debug("PM: writing image.\n");
  550. error = swsusp_write(flags);
  551. swsusp_free();
  552. if (!error)
  553. power_down();
  554. in_suspend = 0;
  555. pm_restore_gfp_mask();
  556. } else {
  557. pr_debug("PM: Image restored successfully.\n");
  558. }
  559. Thaw:
  560. thaw_processes();
  561. Finish:
  562. free_basic_memory_bitmaps();
  563. usermodehelper_enable();
  564. Exit:
  565. pm_notifier_call_chain(PM_POST_HIBERNATION);
  566. pm_restore_console();
  567. atomic_inc(&snapshot_device_available);
  568. Unlock:
  569. unlock_system_sleep();
  570. return error;
  571. }
  572. /**
  573. * software_resume - Resume from a saved hibernation image.
  574. *
  575. * This routine is called as a late initcall, when all devices have been
  576. * discovered and initialized already.
  577. *
  578. * The image reading code is called to see if there is a hibernation image
  579. * available for reading. If that is the case, devices are quiesced and the
  580. * contents of memory is restored from the saved image.
  581. *
  582. * If this is successful, control reappears in the restored target kernel in
  583. * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
  584. * attempts to recover gracefully and make the kernel return to the normal mode
  585. * of operation.
  586. */
  587. static int software_resume(void)
  588. {
  589. int error;
  590. unsigned int flags;
  591. /*
  592. * If the user said "noresume".. bail out early.
  593. */
  594. if (noresume)
  595. return 0;
  596. /*
  597. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  598. * is configured into the kernel. Since the regular hibernate
  599. * trigger path is via sysfs which takes a buffer mutex before
  600. * calling hibernate functions (which take pm_mutex) this can
  601. * cause lockdep to complain about a possible ABBA deadlock
  602. * which cannot happen since we're in the boot code here and
  603. * sysfs can't be invoked yet. Therefore, we use a subclass
  604. * here to avoid lockdep complaining.
  605. */
  606. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  607. if (swsusp_resume_device)
  608. goto Check_image;
  609. if (!strlen(resume_file)) {
  610. error = -ENOENT;
  611. goto Unlock;
  612. }
  613. pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
  614. if (resume_delay) {
  615. printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
  616. resume_delay);
  617. ssleep(resume_delay);
  618. }
  619. /* Check if the device is there */
  620. swsusp_resume_device = name_to_dev_t(resume_file);
  621. if (!swsusp_resume_device) {
  622. /*
  623. * Some device discovery might still be in progress; we need
  624. * to wait for this to finish.
  625. */
  626. wait_for_device_probe();
  627. if (resume_wait) {
  628. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  629. msleep(10);
  630. async_synchronize_full();
  631. }
  632. /*
  633. * We can't depend on SCSI devices being available after loading
  634. * one of their modules until scsi_complete_async_scans() is
  635. * called and the resume device usually is a SCSI one.
  636. */
  637. scsi_complete_async_scans();
  638. swsusp_resume_device = name_to_dev_t(resume_file);
  639. if (!swsusp_resume_device) {
  640. error = -ENODEV;
  641. goto Unlock;
  642. }
  643. }
  644. Check_image:
  645. pr_debug("PM: Hibernation image partition %d:%d present\n",
  646. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  647. pr_debug("PM: Looking for hibernation image.\n");
  648. error = swsusp_check();
  649. if (error)
  650. goto Unlock;
  651. /* The snapshot device should not be opened while we're running */
  652. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  653. error = -EBUSY;
  654. swsusp_close(FMODE_READ);
  655. goto Unlock;
  656. }
  657. pm_prepare_console();
  658. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  659. if (error)
  660. goto close_finish;
  661. error = usermodehelper_disable();
  662. if (error)
  663. goto close_finish;
  664. error = create_basic_memory_bitmaps();
  665. if (error) {
  666. usermodehelper_enable();
  667. goto close_finish;
  668. }
  669. pr_debug("PM: Preparing processes for restore.\n");
  670. error = freeze_processes();
  671. if (error) {
  672. swsusp_close(FMODE_READ);
  673. goto Done;
  674. }
  675. pr_debug("PM: Loading hibernation image.\n");
  676. error = swsusp_read(&flags);
  677. swsusp_close(FMODE_READ);
  678. if (!error)
  679. hibernation_restore(flags & SF_PLATFORM_MODE);
  680. printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
  681. swsusp_free();
  682. thaw_processes();
  683. Done:
  684. free_basic_memory_bitmaps();
  685. usermodehelper_enable();
  686. Finish:
  687. pm_notifier_call_chain(PM_POST_RESTORE);
  688. pm_restore_console();
  689. atomic_inc(&snapshot_device_available);
  690. /* For success case, the suspend path will release the lock */
  691. Unlock:
  692. mutex_unlock(&pm_mutex);
  693. pr_debug("PM: Hibernation image not present or could not be loaded.\n");
  694. return error;
  695. close_finish:
  696. swsusp_close(FMODE_READ);
  697. goto Finish;
  698. }
  699. late_initcall(software_resume);
  700. static const char * const hibernation_modes[] = {
  701. [HIBERNATION_PLATFORM] = "platform",
  702. [HIBERNATION_SHUTDOWN] = "shutdown",
  703. [HIBERNATION_REBOOT] = "reboot",
  704. };
  705. /*
  706. * /sys/power/disk - Control hibernation mode.
  707. *
  708. * Hibernation can be handled in several ways. There are a few different ways
  709. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  710. * or other hibernation_ops), powering it off or rebooting it (for testing
  711. * mostly).
  712. *
  713. * The sysfs file /sys/power/disk provides an interface for selecting the
  714. * hibernation mode to use. Reading from this file causes the available modes
  715. * to be printed. There are 3 modes that can be supported:
  716. *
  717. * 'platform'
  718. * 'shutdown'
  719. * 'reboot'
  720. *
  721. * If a platform hibernation driver is in use, 'platform' will be supported
  722. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  723. * The selected option (i.e. the one corresponding to the current value of
  724. * hibernation_mode) is enclosed by a square bracket.
  725. *
  726. * To select a given hibernation mode it is necessary to write the mode's
  727. * string representation (as returned by reading from /sys/power/disk) back
  728. * into /sys/power/disk.
  729. */
  730. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  731. char *buf)
  732. {
  733. int i;
  734. char *start = buf;
  735. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  736. if (!hibernation_modes[i])
  737. continue;
  738. switch (i) {
  739. case HIBERNATION_SHUTDOWN:
  740. case HIBERNATION_REBOOT:
  741. break;
  742. case HIBERNATION_PLATFORM:
  743. if (hibernation_ops)
  744. break;
  745. /* not a valid mode, continue with loop */
  746. continue;
  747. }
  748. if (i == hibernation_mode)
  749. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  750. else
  751. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  752. }
  753. buf += sprintf(buf, "\n");
  754. return buf-start;
  755. }
  756. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  757. const char *buf, size_t n)
  758. {
  759. int error = 0;
  760. int i;
  761. int len;
  762. char *p;
  763. int mode = HIBERNATION_INVALID;
  764. p = memchr(buf, '\n', n);
  765. len = p ? p - buf : n;
  766. lock_system_sleep();
  767. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  768. if (len == strlen(hibernation_modes[i])
  769. && !strncmp(buf, hibernation_modes[i], len)) {
  770. mode = i;
  771. break;
  772. }
  773. }
  774. if (mode != HIBERNATION_INVALID) {
  775. switch (mode) {
  776. case HIBERNATION_SHUTDOWN:
  777. case HIBERNATION_REBOOT:
  778. hibernation_mode = mode;
  779. break;
  780. case HIBERNATION_PLATFORM:
  781. if (hibernation_ops)
  782. hibernation_mode = mode;
  783. else
  784. error = -EINVAL;
  785. }
  786. } else
  787. error = -EINVAL;
  788. if (!error)
  789. pr_debug("PM: Hibernation mode set to '%s'\n",
  790. hibernation_modes[mode]);
  791. unlock_system_sleep();
  792. return error ? error : n;
  793. }
  794. power_attr(disk);
  795. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  796. char *buf)
  797. {
  798. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  799. MINOR(swsusp_resume_device));
  800. }
  801. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  802. const char *buf, size_t n)
  803. {
  804. unsigned int maj, min;
  805. dev_t res;
  806. int ret = -EINVAL;
  807. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  808. goto out;
  809. res = MKDEV(maj,min);
  810. if (maj != MAJOR(res) || min != MINOR(res))
  811. goto out;
  812. lock_system_sleep();
  813. swsusp_resume_device = res;
  814. unlock_system_sleep();
  815. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  816. noresume = 0;
  817. software_resume();
  818. ret = n;
  819. out:
  820. return ret;
  821. }
  822. power_attr(resume);
  823. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  824. char *buf)
  825. {
  826. return sprintf(buf, "%lu\n", image_size);
  827. }
  828. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  829. const char *buf, size_t n)
  830. {
  831. unsigned long size;
  832. if (sscanf(buf, "%lu", &size) == 1) {
  833. image_size = size;
  834. return n;
  835. }
  836. return -EINVAL;
  837. }
  838. power_attr(image_size);
  839. static ssize_t reserved_size_show(struct kobject *kobj,
  840. struct kobj_attribute *attr, char *buf)
  841. {
  842. return sprintf(buf, "%lu\n", reserved_size);
  843. }
  844. static ssize_t reserved_size_store(struct kobject *kobj,
  845. struct kobj_attribute *attr,
  846. const char *buf, size_t n)
  847. {
  848. unsigned long size;
  849. if (sscanf(buf, "%lu", &size) == 1) {
  850. reserved_size = size;
  851. return n;
  852. }
  853. return -EINVAL;
  854. }
  855. power_attr(reserved_size);
  856. static struct attribute * g[] = {
  857. &disk_attr.attr,
  858. &resume_attr.attr,
  859. &image_size_attr.attr,
  860. &reserved_size_attr.attr,
  861. NULL,
  862. };
  863. static struct attribute_group attr_group = {
  864. .attrs = g,
  865. };
  866. static int __init pm_disk_init(void)
  867. {
  868. return sysfs_create_group(power_kobj, &attr_group);
  869. }
  870. core_initcall(pm_disk_init);
  871. static int __init resume_setup(char *str)
  872. {
  873. if (noresume)
  874. return 1;
  875. strncpy( resume_file, str, 255 );
  876. return 1;
  877. }
  878. static int __init resume_offset_setup(char *str)
  879. {
  880. unsigned long long offset;
  881. if (noresume)
  882. return 1;
  883. if (sscanf(str, "%llu", &offset) == 1)
  884. swsusp_resume_block = offset;
  885. return 1;
  886. }
  887. static int __init hibernate_setup(char *str)
  888. {
  889. if (!strncmp(str, "noresume", 8))
  890. noresume = 1;
  891. else if (!strncmp(str, "nocompress", 10))
  892. nocompress = 1;
  893. return 1;
  894. }
  895. static int __init noresume_setup(char *str)
  896. {
  897. noresume = 1;
  898. return 1;
  899. }
  900. static int __init resumewait_setup(char *str)
  901. {
  902. resume_wait = 1;
  903. return 1;
  904. }
  905. static int __init resumedelay_setup(char *str)
  906. {
  907. resume_delay = simple_strtoul(str, NULL, 0);
  908. return 1;
  909. }
  910. __setup("noresume", noresume_setup);
  911. __setup("resume_offset=", resume_offset_setup);
  912. __setup("resume=", resume_setup);
  913. __setup("hibernate=", hibernate_setup);
  914. __setup("resumewait", resumewait_setup);
  915. __setup("resumedelay=", resumedelay_setup);