hibernate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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/delay.h>
  19. #include <linux/fs.h>
  20. #include <linux/mount.h>
  21. #include <linux/pm.h>
  22. #include <linux/console.h>
  23. #include <linux/cpu.h>
  24. #include <linux/freezer.h>
  25. #include <linux/gfp.h>
  26. #include <linux/syscore_ops.h>
  27. #include <linux/ctype.h>
  28. #include <linux/genhd.h>
  29. #include <scsi/scsi_scan.h>
  30. #include "power.h"
  31. static int nocompress;
  32. static int noresume;
  33. static int resume_wait;
  34. static int resume_delay;
  35. static char resume_file[256] = CONFIG_PM_STD_PARTITION;
  36. dev_t swsusp_resume_device;
  37. sector_t swsusp_resume_block;
  38. int in_suspend __nosavedata;
  39. enum {
  40. HIBERNATION_INVALID,
  41. HIBERNATION_PLATFORM,
  42. HIBERNATION_SHUTDOWN,
  43. HIBERNATION_REBOOT,
  44. /* keep last */
  45. __HIBERNATION_AFTER_LAST
  46. };
  47. #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
  48. #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
  49. static int hibernation_mode = HIBERNATION_SHUTDOWN;
  50. bool freezer_test_done;
  51. static const struct platform_hibernation_ops *hibernation_ops;
  52. /**
  53. * hibernation_set_ops - Set the global hibernate operations.
  54. * @ops: Hibernation operations to use in subsequent hibernation transitions.
  55. */
  56. void hibernation_set_ops(const struct platform_hibernation_ops *ops)
  57. {
  58. if (ops && !(ops->begin && ops->end && ops->pre_snapshot
  59. && ops->prepare && ops->finish && ops->enter && ops->pre_restore
  60. && ops->restore_cleanup && ops->leave)) {
  61. WARN_ON(1);
  62. return;
  63. }
  64. lock_system_sleep();
  65. hibernation_ops = ops;
  66. if (ops)
  67. hibernation_mode = HIBERNATION_PLATFORM;
  68. else if (hibernation_mode == HIBERNATION_PLATFORM)
  69. hibernation_mode = HIBERNATION_SHUTDOWN;
  70. unlock_system_sleep();
  71. }
  72. static bool entering_platform_hibernation;
  73. bool system_entering_hibernation(void)
  74. {
  75. return entering_platform_hibernation;
  76. }
  77. EXPORT_SYMBOL(system_entering_hibernation);
  78. #ifdef CONFIG_PM_DEBUG
  79. static void hibernation_debug_sleep(void)
  80. {
  81. printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
  82. mdelay(5000);
  83. }
  84. static int hibernation_test(int level)
  85. {
  86. if (pm_test_level == level) {
  87. hibernation_debug_sleep();
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. #else /* !CONFIG_PM_DEBUG */
  93. static int hibernation_test(int level) { return 0; }
  94. #endif /* !CONFIG_PM_DEBUG */
  95. /**
  96. * platform_begin - Call platform to start hibernation.
  97. * @platform_mode: Whether or not to use the platform driver.
  98. */
  99. static int platform_begin(int platform_mode)
  100. {
  101. return (platform_mode && hibernation_ops) ?
  102. hibernation_ops->begin() : 0;
  103. }
  104. /**
  105. * platform_end - Call platform to finish transition to the working state.
  106. * @platform_mode: Whether or not to use the platform driver.
  107. */
  108. static void platform_end(int platform_mode)
  109. {
  110. if (platform_mode && hibernation_ops)
  111. hibernation_ops->end();
  112. }
  113. /**
  114. * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
  115. * @platform_mode: Whether or not to use the platform driver.
  116. *
  117. * Use the platform driver to prepare the system for creating a hibernate image,
  118. * if so configured, and return an error code if that fails.
  119. */
  120. static int platform_pre_snapshot(int platform_mode)
  121. {
  122. return (platform_mode && hibernation_ops) ?
  123. hibernation_ops->pre_snapshot() : 0;
  124. }
  125. /**
  126. * platform_leave - Call platform to prepare a transition to the working state.
  127. * @platform_mode: Whether or not to use the platform driver.
  128. *
  129. * Use the platform driver prepare to prepare the machine for switching to the
  130. * normal mode of operation.
  131. *
  132. * This routine is called on one CPU with interrupts disabled.
  133. */
  134. static void platform_leave(int platform_mode)
  135. {
  136. if (platform_mode && hibernation_ops)
  137. hibernation_ops->leave();
  138. }
  139. /**
  140. * platform_finish - Call platform to switch the system to the working state.
  141. * @platform_mode: Whether or not to use the platform driver.
  142. *
  143. * Use the platform driver to switch the machine to the normal mode of
  144. * operation.
  145. *
  146. * This routine must be called after platform_prepare().
  147. */
  148. static void platform_finish(int platform_mode)
  149. {
  150. if (platform_mode && hibernation_ops)
  151. hibernation_ops->finish();
  152. }
  153. /**
  154. * platform_pre_restore - Prepare for hibernate image restoration.
  155. * @platform_mode: Whether or not to use the platform driver.
  156. *
  157. * Use the platform driver to prepare the system for resume from a hibernation
  158. * image.
  159. *
  160. * If the restore fails after this function has been called,
  161. * platform_restore_cleanup() must be called.
  162. */
  163. static int platform_pre_restore(int platform_mode)
  164. {
  165. return (platform_mode && hibernation_ops) ?
  166. hibernation_ops->pre_restore() : 0;
  167. }
  168. /**
  169. * platform_restore_cleanup - Switch to the working state after failing restore.
  170. * @platform_mode: Whether or not to use the platform driver.
  171. *
  172. * Use the platform driver to switch the system to the normal mode of operation
  173. * after a failing restore.
  174. *
  175. * If platform_pre_restore() has been called before the failing restore, this
  176. * function must be called too, regardless of the result of
  177. * platform_pre_restore().
  178. */
  179. static void platform_restore_cleanup(int platform_mode)
  180. {
  181. if (platform_mode && hibernation_ops)
  182. hibernation_ops->restore_cleanup();
  183. }
  184. /**
  185. * platform_recover - Recover from a failure to suspend devices.
  186. * @platform_mode: Whether or not to use the platform driver.
  187. */
  188. static void platform_recover(int platform_mode)
  189. {
  190. if (platform_mode && hibernation_ops && hibernation_ops->recover)
  191. hibernation_ops->recover();
  192. }
  193. /**
  194. * swsusp_show_speed - Print time elapsed between two events during hibernation.
  195. * @start: Starting event.
  196. * @stop: Final event.
  197. * @nr_pages: Number of memory pages processed between @start and @stop.
  198. * @msg: Additional diagnostic message to print.
  199. */
  200. void swsusp_show_speed(struct timeval *start, struct timeval *stop,
  201. unsigned nr_pages, char *msg)
  202. {
  203. s64 elapsed_centisecs64;
  204. int centisecs;
  205. int k;
  206. int kps;
  207. elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
  208. do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
  209. centisecs = elapsed_centisecs64;
  210. if (centisecs == 0)
  211. centisecs = 1; /* avoid div-by-zero */
  212. k = nr_pages * (PAGE_SIZE / 1024);
  213. kps = (k * 100) / centisecs;
  214. printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
  215. msg, k,
  216. centisecs / 100, centisecs % 100,
  217. kps / 1000, (kps % 1000) / 10);
  218. }
  219. /**
  220. * create_image - Create a hibernation image.
  221. * @platform_mode: Whether or not to use the platform driver.
  222. *
  223. * Execute device drivers' "late" and "noirq" freeze callbacks, create a
  224. * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
  225. *
  226. * Control reappears in this routine after the subsequent restore.
  227. */
  228. static int create_image(int platform_mode)
  229. {
  230. int error;
  231. error = dpm_suspend_end(PMSG_FREEZE);
  232. if (error) {
  233. printk(KERN_ERR "PM: Some devices failed to power down, "
  234. "aborting hibernation\n");
  235. return error;
  236. }
  237. error = platform_pre_snapshot(platform_mode);
  238. if (error || hibernation_test(TEST_PLATFORM))
  239. goto Platform_finish;
  240. error = disable_nonboot_cpus();
  241. if (error || hibernation_test(TEST_CPUS))
  242. goto Enable_cpus;
  243. local_irq_disable();
  244. error = syscore_suspend();
  245. if (error) {
  246. printk(KERN_ERR "PM: Some system devices failed to power down, "
  247. "aborting hibernation\n");
  248. goto Enable_irqs;
  249. }
  250. if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
  251. goto Power_up;
  252. in_suspend = 1;
  253. save_processor_state();
  254. error = swsusp_arch_suspend();
  255. if (error)
  256. printk(KERN_ERR "PM: Error %d creating hibernation image\n",
  257. error);
  258. /* Restore control flow magically appears here */
  259. restore_processor_state();
  260. if (!in_suspend) {
  261. events_check_enabled = false;
  262. platform_leave(platform_mode);
  263. }
  264. Power_up:
  265. syscore_resume();
  266. Enable_irqs:
  267. local_irq_enable();
  268. Enable_cpus:
  269. enable_nonboot_cpus();
  270. Platform_finish:
  271. platform_finish(platform_mode);
  272. dpm_resume_start(in_suspend ?
  273. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  274. return error;
  275. }
  276. /**
  277. * hibernation_snapshot - Quiesce devices and create a hibernation image.
  278. * @platform_mode: If set, use platform driver to prepare for the transition.
  279. *
  280. * This routine must be called with pm_mutex held.
  281. */
  282. int hibernation_snapshot(int platform_mode)
  283. {
  284. pm_message_t msg;
  285. int error;
  286. error = platform_begin(platform_mode);
  287. if (error)
  288. goto Close;
  289. /* Preallocate image memory before shutting down devices. */
  290. error = hibernate_preallocate_memory();
  291. if (error)
  292. goto Close;
  293. error = freeze_kernel_threads();
  294. if (error)
  295. goto Cleanup;
  296. if (hibernation_test(TEST_FREEZER)) {
  297. /*
  298. * Indicate to the caller that we are returning due to a
  299. * successful freezer test.
  300. */
  301. freezer_test_done = true;
  302. goto Thaw;
  303. }
  304. error = dpm_prepare(PMSG_FREEZE);
  305. if (error) {
  306. dpm_complete(PMSG_RECOVER);
  307. goto Thaw;
  308. }
  309. suspend_console();
  310. pm_restrict_gfp_mask();
  311. error = dpm_suspend(PMSG_FREEZE);
  312. if (error || hibernation_test(TEST_DEVICES))
  313. platform_recover(platform_mode);
  314. else
  315. error = create_image(platform_mode);
  316. /*
  317. * In the case that we call create_image() above, the control
  318. * returns here (1) after the image has been created or the
  319. * image creation has failed and (2) after a successful restore.
  320. */
  321. /* We may need to release the preallocated image pages here. */
  322. if (error || !in_suspend)
  323. swsusp_free();
  324. msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
  325. dpm_resume(msg);
  326. if (error || !in_suspend)
  327. pm_restore_gfp_mask();
  328. resume_console();
  329. dpm_complete(msg);
  330. Close:
  331. platform_end(platform_mode);
  332. return error;
  333. Thaw:
  334. thaw_kernel_threads();
  335. Cleanup:
  336. swsusp_free();
  337. goto Close;
  338. }
  339. /**
  340. * resume_target_kernel - Restore system state from a hibernation image.
  341. * @platform_mode: Whether or not to use the platform driver.
  342. *
  343. * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
  344. * contents of highmem that have not been restored yet from the image and run
  345. * the low-level code that will restore the remaining contents of memory and
  346. * switch to the just restored target kernel.
  347. */
  348. static int resume_target_kernel(bool platform_mode)
  349. {
  350. int error;
  351. error = dpm_suspend_end(PMSG_QUIESCE);
  352. if (error) {
  353. printk(KERN_ERR "PM: Some devices failed to power down, "
  354. "aborting resume\n");
  355. return error;
  356. }
  357. error = platform_pre_restore(platform_mode);
  358. if (error)
  359. goto Cleanup;
  360. error = disable_nonboot_cpus();
  361. if (error)
  362. goto Enable_cpus;
  363. local_irq_disable();
  364. error = syscore_suspend();
  365. if (error)
  366. goto Enable_irqs;
  367. save_processor_state();
  368. error = restore_highmem();
  369. if (!error) {
  370. error = swsusp_arch_resume();
  371. /*
  372. * The code below is only ever reached in case of a failure.
  373. * Otherwise, execution continues at the place where
  374. * swsusp_arch_suspend() was called.
  375. */
  376. BUG_ON(!error);
  377. /*
  378. * This call to restore_highmem() reverts the changes made by
  379. * the previous one.
  380. */
  381. restore_highmem();
  382. }
  383. /*
  384. * The only reason why swsusp_arch_resume() can fail is memory being
  385. * very tight, so we have to free it as soon as we can to avoid
  386. * subsequent failures.
  387. */
  388. swsusp_free();
  389. restore_processor_state();
  390. touch_softlockup_watchdog();
  391. syscore_resume();
  392. Enable_irqs:
  393. local_irq_enable();
  394. Enable_cpus:
  395. enable_nonboot_cpus();
  396. Cleanup:
  397. platform_restore_cleanup(platform_mode);
  398. dpm_resume_start(PMSG_RECOVER);
  399. return error;
  400. }
  401. /**
  402. * hibernation_restore - Quiesce devices and restore from a hibernation image.
  403. * @platform_mode: If set, use platform driver to prepare for the transition.
  404. *
  405. * This routine must be called with pm_mutex held. If it is successful, control
  406. * reappears in the restored target kernel in hibernation_snapshot().
  407. */
  408. int hibernation_restore(int platform_mode)
  409. {
  410. int error;
  411. pm_prepare_console();
  412. suspend_console();
  413. pm_restrict_gfp_mask();
  414. error = dpm_suspend_start(PMSG_QUIESCE);
  415. if (!error) {
  416. error = resume_target_kernel(platform_mode);
  417. dpm_resume_end(PMSG_RECOVER);
  418. }
  419. pm_restore_gfp_mask();
  420. resume_console();
  421. pm_restore_console();
  422. return error;
  423. }
  424. /**
  425. * hibernation_platform_enter - Power off the system using the platform driver.
  426. */
  427. int hibernation_platform_enter(void)
  428. {
  429. int error;
  430. if (!hibernation_ops)
  431. return -ENOSYS;
  432. /*
  433. * We have cancelled the power transition by running
  434. * hibernation_ops->finish() before saving the image, so we should let
  435. * the firmware know that we're going to enter the sleep state after all
  436. */
  437. error = hibernation_ops->begin();
  438. if (error)
  439. goto Close;
  440. entering_platform_hibernation = true;
  441. suspend_console();
  442. error = dpm_suspend_start(PMSG_HIBERNATE);
  443. if (error) {
  444. if (hibernation_ops->recover)
  445. hibernation_ops->recover();
  446. goto Resume_devices;
  447. }
  448. error = dpm_suspend_end(PMSG_HIBERNATE);
  449. if (error)
  450. goto Resume_devices;
  451. error = hibernation_ops->prepare();
  452. if (error)
  453. goto Platform_finish;
  454. error = disable_nonboot_cpus();
  455. if (error)
  456. goto Platform_finish;
  457. local_irq_disable();
  458. syscore_suspend();
  459. if (pm_wakeup_pending()) {
  460. error = -EAGAIN;
  461. goto Power_up;
  462. }
  463. hibernation_ops->enter();
  464. /* We should never get here */
  465. while (1);
  466. Power_up:
  467. syscore_resume();
  468. local_irq_enable();
  469. enable_nonboot_cpus();
  470. Platform_finish:
  471. hibernation_ops->finish();
  472. dpm_resume_start(PMSG_RESTORE);
  473. Resume_devices:
  474. entering_platform_hibernation = false;
  475. dpm_resume_end(PMSG_RESTORE);
  476. resume_console();
  477. Close:
  478. hibernation_ops->end();
  479. return error;
  480. }
  481. /**
  482. * power_down - Shut the machine down for hibernation.
  483. *
  484. * Use the platform driver, if configured, to put the system into the sleep
  485. * state corresponding to hibernation, or try to power it off or reboot,
  486. * depending on the value of hibernation_mode.
  487. */
  488. static void power_down(void)
  489. {
  490. switch (hibernation_mode) {
  491. case HIBERNATION_REBOOT:
  492. kernel_restart(NULL);
  493. break;
  494. case HIBERNATION_PLATFORM:
  495. hibernation_platform_enter();
  496. case HIBERNATION_SHUTDOWN:
  497. kernel_power_off();
  498. break;
  499. }
  500. kernel_halt();
  501. /*
  502. * Valid image is on the disk, if we continue we risk serious data
  503. * corruption after resume.
  504. */
  505. printk(KERN_CRIT "PM: Please power down manually\n");
  506. while(1);
  507. }
  508. /**
  509. * hibernate - Carry out system hibernation, including saving the image.
  510. */
  511. int hibernate(void)
  512. {
  513. int error;
  514. lock_system_sleep();
  515. /* The snapshot device should not be opened while we're running */
  516. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  517. error = -EBUSY;
  518. goto Unlock;
  519. }
  520. pm_prepare_console();
  521. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  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 Free_bitmaps;
  534. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  535. if (error || freezer_test_done)
  536. goto Thaw;
  537. if (in_suspend) {
  538. unsigned int flags = 0;
  539. if (hibernation_mode == HIBERNATION_PLATFORM)
  540. flags |= SF_PLATFORM_MODE;
  541. if (nocompress)
  542. flags |= SF_NOCOMPRESS_MODE;
  543. else
  544. flags |= SF_CRC32_MODE;
  545. pr_debug("PM: writing image.\n");
  546. error = swsusp_write(flags);
  547. swsusp_free();
  548. if (!error)
  549. power_down();
  550. in_suspend = 0;
  551. pm_restore_gfp_mask();
  552. } else {
  553. pr_debug("PM: Image restored successfully.\n");
  554. }
  555. Thaw:
  556. thaw_processes();
  557. /* Don't bother checking whether freezer_test_done is true */
  558. freezer_test_done = false;
  559. Free_bitmaps:
  560. free_basic_memory_bitmaps();
  561. Exit:
  562. pm_notifier_call_chain(PM_POST_HIBERNATION);
  563. pm_restore_console();
  564. atomic_inc(&snapshot_device_available);
  565. Unlock:
  566. unlock_system_sleep();
  567. return error;
  568. }
  569. /**
  570. * software_resume - Resume from a saved hibernation image.
  571. *
  572. * This routine is called as a late initcall, when all devices have been
  573. * discovered and initialized already.
  574. *
  575. * The image reading code is called to see if there is a hibernation image
  576. * available for reading. If that is the case, devices are quiesced and the
  577. * contents of memory is restored from the saved image.
  578. *
  579. * If this is successful, control reappears in the restored target kernel in
  580. * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
  581. * attempts to recover gracefully and make the kernel return to the normal mode
  582. * of operation.
  583. */
  584. static int software_resume(void)
  585. {
  586. int error;
  587. unsigned int flags;
  588. /*
  589. * If the user said "noresume".. bail out early.
  590. */
  591. if (noresume)
  592. return 0;
  593. /*
  594. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  595. * is configured into the kernel. Since the regular hibernate
  596. * trigger path is via sysfs which takes a buffer mutex before
  597. * calling hibernate functions (which take pm_mutex) this can
  598. * cause lockdep to complain about a possible ABBA deadlock
  599. * which cannot happen since we're in the boot code here and
  600. * sysfs can't be invoked yet. Therefore, we use a subclass
  601. * here to avoid lockdep complaining.
  602. */
  603. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  604. if (swsusp_resume_device)
  605. goto Check_image;
  606. if (!strlen(resume_file)) {
  607. error = -ENOENT;
  608. goto Unlock;
  609. }
  610. pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
  611. if (resume_delay) {
  612. printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
  613. resume_delay);
  614. ssleep(resume_delay);
  615. }
  616. /* Check if the device is there */
  617. swsusp_resume_device = name_to_dev_t(resume_file);
  618. /*
  619. * name_to_dev_t is ineffective to verify parition if resume_file is in
  620. * integer format. (e.g. major:minor)
  621. */
  622. if (isdigit(resume_file[0]) && resume_wait) {
  623. int partno;
  624. while (!get_gendisk(swsusp_resume_device, &partno))
  625. msleep(10);
  626. }
  627. if (!swsusp_resume_device) {
  628. /*
  629. * Some device discovery might still be in progress; we need
  630. * to wait for this to finish.
  631. */
  632. wait_for_device_probe();
  633. if (resume_wait) {
  634. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  635. msleep(10);
  636. async_synchronize_full();
  637. }
  638. /*
  639. * We can't depend on SCSI devices being available after loading
  640. * one of their modules until scsi_complete_async_scans() is
  641. * called and the resume device usually is a SCSI one.
  642. */
  643. scsi_complete_async_scans();
  644. swsusp_resume_device = name_to_dev_t(resume_file);
  645. if (!swsusp_resume_device) {
  646. error = -ENODEV;
  647. goto Unlock;
  648. }
  649. }
  650. Check_image:
  651. pr_debug("PM: Hibernation image partition %d:%d present\n",
  652. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  653. pr_debug("PM: Looking for hibernation image.\n");
  654. error = swsusp_check();
  655. if (error)
  656. goto Unlock;
  657. /* The snapshot device should not be opened while we're running */
  658. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  659. error = -EBUSY;
  660. swsusp_close(FMODE_READ);
  661. goto Unlock;
  662. }
  663. pm_prepare_console();
  664. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  665. if (error)
  666. goto close_finish;
  667. error = create_basic_memory_bitmaps();
  668. if (error)
  669. goto close_finish;
  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. 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);