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