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