hibernate.c 26 KB

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