hibernate.c 26 KB

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